//命名空间using System.Net.Mail; /// <summary> /// 发送邮件 /// </summary> /// <param name="fromemail">发件人邮箱</param> /// <param name="pwd">发件人密码</param> /// <param name="toemail">收件人邮箱</param> /// <param name="subject">主题</param> /// <param name="body">内容</param>
/// <param name="file">附件</param>
/// <returns></returns> public static bool send(string fromemail, string pwd, string toemail, string subject, string body,string file) {
Attachment objMailAttachment = new Attachment(file);//发送邮件的附件 SmtpClient client = new SmtpClient(); client.Host = "smtp." + fromemail.Remove(0, fromemail.IndexOf("@") + 1); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(fromemail, pwd); client.DeliveryMethod = SmtpDeliveryMethod.Network; MailMessage message = new MailMessage(fromemail, toemail);
message.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中 message.Subject = subject; message.Body = body; message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; try { client.Send(message); return true; } catch { throw; } }
不一样的
public bool SendMail(string smtpserver, string username, string pwd, MailMessage MailIn) { SmtpClient mailClient = null; try { mailClient = new SmtpClient(smtpserver); mailClient.UseDefaultCredentials = false; //if (!string.IsNullOrEmpty(username)) mailClient.Credentials = new NetworkCredential(username, pwd); mailClient.DeliveryMethod = SmtpDeliveryMethod.Network; //mailClient.Timeout = 50;//同步发送邮件的超时时间。 mailClient.Timeout = 50000; //if (MailIn == null || ((MailIn.To == null || MailIn.To.Count == 0))) // return false; mailClient.Send(MailIn); } catch (Exception e) { //记录日志。 //LogHelper.Error("MailHandler", "邮件发送失败:" + e.Message); //WriteLogFile("邮件发送失败:" + e.Message); return false; } return true; }
string fromemail = SkyCenter.Config.SettingsProvider.Instance().GetSettingsInfo("SmtpUsername").KeyValue; string pwd = SkyCenter.Config.SettingsProvider.Instance().GetSettingsInfo("SmtpPassword").KeyValue; string smtp = SkyCenter.Config.SettingsProvider.Instance().GetSettingsInfo("SmtpServer").KeyValue; string toemail = SkyCenter.Config.SettingsProvider.Instance().GetSettingsInfo("WebMasterEmail").KeyValue; MailMessage message = new MailMessage(fromemail, toemail); Attachment objMailAttachment = new Attachment(Information);//发送邮件的附件 message.Subject = "品牌"; message.Body = sb.ToString(); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中 if (SendMail(smtp, fromemail.Substring(0, fromemail.IndexOf("@")), pwd, message)) { //if (send("品牌", sb.ToString(), Information)) //{ base.Alert("品牌成功,已发送邮件!"); }