直接贴代码了:
public static bool SendMail(string emailTo, string emailTitle, string emailContent , out string errorMessage, string[] filePath = null, string[] additionalHeaders = null) { WebMail.SmtpServer = "smtp.gmail.com";//获取或设置要用于发送电子邮件的 SMTP 中继邮件服务器的名称。 WebMail.SmtpPort = 25;//发送端口 WebMail.EnableSsl = true;//是否启用 SSL GMAIL 需要 而其他都不需要 具体看你在邮箱中的配置 WebMail.UserName = "xxx";//账号名 WebMail.From = "xxx@gmail.com";//邮箱名 WebMail.Password = "xxx123456";//密码 WebMail.SmtpUseDefaultCredentials = true;//是否使用默认配置 errorMessage = null; try { WebMail.Send(to: emailTo, subject: emailTitle, body: emailContent, isBodyHtml: true, filesToAttach: filePath, additionalHeaders: additionalHeaders); return true; } catch (Exception e) { errorMessage = e.Message; return false; } } public static bool SendMail(string emailTo, string emailTitle, string emailContent, out string errorMsg) { bool flag = SendMail(emailTo, emailTitle, emailContent, out errorMsg); return flag; }
谢谢浏览!