1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| public class MailHelper { public static readonly string MailName = "邮件中心"; public static readonly string body = "XXXXX";
public static string send(string toMail, string MailServer, string MailUserName, string MailPassword, int Port, bool EnableSsl) { try { SmtpClient smtpClient = new SmtpClient(); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = EnableSsl; smtpClient.Host = MailServer; smtpClient.Port = Port; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = (ICredentialsByHost)new NetworkCredential(MailUserName, MailPassword); MailMessage message = new MailMessage() { Priority = MailPriority.Normal, From = new MailAddress(MailUserName, "XXXX", Encoding.UTF8), Subject = "主题", SubjectEncoding = Encoding.UTF8, To = { toMail } }; message.SubjectEncoding = Encoding.GetEncoding(936); message.IsBodyHtml = true; message.BodyEncoding = Encoding.GetEncoding(936); message.Body = "<font color='red'>" + MailHelper.body + "</font><br>"; smtpClient.Send(message); return "邮件发送成功"; } catch (Exception ex) { return ex.Message; } }
public static string send1(string toMail, string MailServer, string MailUserName, string MailPassword, int Port, bool EnableSsl) { System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage(); //邮件主题 mmsg.Subject = "hello, world"; mmsg.BodyFormat = System.Web.Mail.MailFormat.Html; //邮件正文 mmsg.Body = "hello, world。哈哈哈哈哈哈,听说会被屏蔽掉的"; //正文编码 mmsg.BodyEncoding = Encoding.UTF8; //优先级 mmsg.Priority = System.Web.Mail.MailPriority.Normal; //发件者邮箱地址 mmsg.From = MailUserName; //收件人收箱地址 mmsg.To = toMail;
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //用户名 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailUserName); //密码 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailPassword); //端口 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Port); //是否ssl mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", EnableSsl); //Smtp服务器 System.Web.Mail.SmtpMail.SmtpServer = MailServer;
try { System.Web.Mail.SmtpMail.Send(mmsg); return "发送成功"; } catch (Exception ex) { return "发送失败,失败原因:" + ex.Message; } } }
|