我不是依靠主机发送电子邮件,而是考虑使用个人Gmail账户发送电子邮件。 这些电子邮件是发给我在演出中演奏的乐队的个性化电子邮件。 有可能吗? html
这是发送带有附件的电子邮件。 web
来源: http : //coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html 服务器
using System.Net; using System.Net.Mail; public void email_send() { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your mail@gmail.com"); mail.To.Add("to_mail@gmail.com"); mail.Subject = "Test Mail - 1"; mail.Body = "mail with attachment"; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment("c:/textfile.txt"); mail.Attachments.Add(attachment); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); }
来源 : 在ASP.NET C#中发送电子邮件 app
如下是使用C#发送邮件的示例工做代码,在如下示例中,我使用的是Google的smtp服务器。 网站
该代码很容易解释,用您的电子邮件和密码值替换电子邮件和密码。 google
public void SendEmail(string address, string subject, string message) { string email = "yrshaikh.mail@gmail.com"; string password = "put-your-GMAIL-password-here"; var loginInfo = new NetworkCredential(email, password); var msg = new MailMessage(); var smtpClient = new SmtpClient("smtp.gmail.com", 587); msg.From = new MailAddress(email); msg.To.Add(new MailAddress(address)); msg.Subject = subject; msg.Body = message; msg.IsBodyHtml = true; smtpClient.EnableSsl = true; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = loginInfo; smtpClient.Send(msg); }
更改Gmail / Outlook.com电子邮件上的发件人: spa
为防止欺骗-Gmail / Outlook.com不容许您从任意用户账户名发送邮件。 .net
若是发件人数量有限,则能够按照如下说明进行操做,而后将“ From
字段设置为此地址: 从其余地址发送邮件 code
若是您想从任意电子邮件地址发送邮件(例如,用户输入电子邮件的网站上的反馈表,而且您不但愿他们直接经过电子邮件发送给您),则能够采起的最佳措施是: htm
msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
这样一来,您只需在电子邮件账户中点击“回复”,便可在反馈页面上回复乐队的支持者,但他们却收不到您的实际电子邮件,这可能会致使大量垃圾邮件。
若是您处于受控环境中,则效果很好,可是请注意,即便指定了回复对象,我也看到一些客户端将电子邮件发送到发件人地址(我不知道是哪一个)。
若是要发送后台电子邮件,请执行如下操做
public void SendEmail(string address, string subject, string message) { Thread threadSendMails; threadSendMails = new Thread(delegate() { //Place your Code here }); threadSendMails.IsBackground = true; threadSendMails.Start(); }
并添加名称空间
using System.Threading;
这是一种发送邮件并从web.config获取凭据的方法:
public static string SendEmail(string To, string Subject, string Msg, bool bodyHtml = false, bool test = false, Stream AttachmentStream = null, string AttachmentType = null, string AttachmentFileName = null) { try { System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg); newMsg.BodyEncoding = System.Text.Encoding.UTF8; newMsg.HeadersEncoding = System.Text.Encoding.UTF8; newMsg.SubjectEncoding = System.Text.Encoding.UTF8; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null) { System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName); System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition; disposition.FileName = AttachmentFileName; disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment; newMsg.Attachments.Add(attachment); } if (test) { smtpClient.PickupDirectoryLocation = "C:\\TestEmail"; smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory; } else { //smtpClient.EnableSsl = true; } newMsg.IsBodyHtml = bodyHtml; smtpClient.Send(newMsg); return SENT_OK; } catch (Exception ex) { return "Error: " + ex.Message + "<br/><br/>Inner Exception: " + ex.InnerException; } }
以及web.config中的相应部分:
<appSettings> <add key="mailCfg" value="yourmail@example.com"/> </appSettings> <system.net> <mailSettings> <smtp deliveryMethod="Network" from="yourmail@example.com"> <network defaultCredentials="false" host="mail.exapmple.com" userName="yourmail@example.com" password="your_password" port="25"/> </smtp> </mailSettings> </system.net>