C# change e-mail 'from' address to a user-provided one
We have an app that allows users to send e-mails from our system. It allows the user to specify their e-mail address, and gives them several standard templates to use as a starting point for their e-mail.
When we send the e-mails, we use the address they provided as the 'reply-to', but the 'from' address of the e-mail (naturally) looks like our system (from 'submit@ourserver.com').
Is there a way to change this without getting tangled up in spam filters or automatic blocking? We'd prefer no开发者_Go百科t to confuse the recipient as to who actually composed the e-mail they've received.
I'll refer you to Jeff Atwood's Coding Horror article about sending e-mail programattically. It describes in lengths the steps you should take to prevent your e-mail from being caught in spam filters, etc...
Jeff Atwood's Coding Horror: So You'd Like to Send Some Email (Through Code)
I use this code:
public static bool sendEmail(string fromName, string fromEmail, string body, string subject, string toEmail) {
String strReplyTo = fromEmail.Trim();
String strTo = toEmail;
String msgBodyTop = "Email from: " + @fromName + "(" + @fromEmail + ")\n"
+ "" + " " + DateTime.Now.ToLongTimeString()
+ " FROM " + HttpContext.Current.Request.Url.ToString + " : \n\n"
+ "---\n";
MailMessage theMail = new MailMessage(fromEmail, strTo, subject, msgBodyTop + body);
theMail.From = new MailAddress(strReplyTo, fromName);
SmtpClient theClient = new SmtpClient(ConfigurationManager.AppSettings["SMTP"].ToString());
theClient.Send(theMail);
return true;
}
It seems to work for me...
After discussing with our ops people and trying Atomiton's method, I've found that this is not actually possible for us.
精彩评论