Share via


How to send an email from any email to anyone in c#?

Question

Wednesday, November 4, 2015 12:33 AM

Hi Team,

I want to create email sending application where i need to setup from email as any email and receiver email also any email. It means From email could be gmail,yahoo, @otherexample.com.

Do we have any provision in c# that we can send email to anyone.

Please suggest.

Thanks.

All replies (9)

Thursday, November 5, 2015 11:14 AM ✅Answered

Thanks All.....

I am looking for Open Source SMTP server which can support any from email to any to email.

I want source code which can support any from email, auto detect server and send an email.

I have used gmail and yahoo host but they are specific to them.

Please suggest.

in general this will no longer work. the email relay servers will detect your mail as forged (which it is), and automatically put your smtp server on the spammer list, and refuse to deliver the mail.  

what your are trying to do is called email spoofing, and as this is now considered a crime, you should question your requirements.


Wednesday, November 4, 2015 1:04 AM

Do we have any provision in c# that we can send email to anyone.

Didn't understand your question completely. You can use the below code to send email from c#

using System.Net.Mail;

...

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

Ref: http://stackoverflow.com/questions/9201239/send-e-mail-via-smtp-using-c-sharp


Wednesday, November 4, 2015 1:34 AM

Hi Mangesh ,

It means From email could be gmail,yahoo, @otherexample.com.

You could send email by SMTP ,code below is sending Email from Yahoo!, GMail, Hotmail :

string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "[email protected]";
string password = "abcdefg";
string emailTo = "[email protected]";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    // Can set to false, if you are sending pure text.

    mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
    mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}

In addition ,port 587 is supported by almost every outgoing SMTP server and it's useful for unencrypted or TLS connections; while port 465 is the right choice if you need to connect via SSL.

Best Regards,

Nan Yu


Wednesday, November 4, 2015 1:36 AM

Mike (mikesdotnetting) got a nice article here : http://www.mikesdotnetting.com/article/268/how-to-send-email-in-asp-net-mvc

Myself would not recommend sending an email from within mvc (or any web application). Blocks the request, and since it does not need to happen *at this instant* can be done somewhere else.


Wednesday, November 4, 2015 6:59 AM

Hi,

Sure. A simple search should have given tons of code samples showing how to send a mail. The "From" address is not always configurable depending on your mail server settings (for example with gmail, what you defined is overriden with the actual gmail address likely for safety reasons).

If you have tried something it would be best to tell which problem you have.


Wednesday, November 4, 2015 7:22 AM

I want to create email sending application where i need to setup from email as any email and receiver email also any email. It means From email could be gmail,yahoo, @otherexample.com.

it depends on the smtp server you use some smtp servers block email adresses to their domain some do not but public ones allowing this are used for spamming and therefore mostly blacklisted. the code for sending these emails does not change. you can setup a smtp server on your iis server and send from there it will be able to send emails with all possible domains and even impossible :)


Wednesday, November 4, 2015 10:17 AM

As stated you need a smtp server that allows this, but in general most of the mail will be blocked as spam, because the sending domain does not match the from address. Actually the smtp server you use, should also be detected as a Spammer, so all email from it will be blocked. So be careful whose smtp server you use.


Thursday, November 5, 2015 2:23 AM

Thanks All.....

I am looking for Open Source SMTP server which can support any from email to any to email.

I want source code which can support any from email, auto detect server and send an email.

I have used gmail and yahoo host but they are specific to them.

Please suggest.


Thursday, November 5, 2015 3:10 AM

Mangesh4235

I am looking for Open Source SMTP server which can support any from email to any to email.

an opensource smtpd is hard to find for windows but if you have a linux/unix system available:

https://www.opensmtpd.org/

altough i would suggest you take a look at https://www.mailgun.com/ i think the first 10k mails/month are free and i think you can send from any domain.