发送Email 的条件
1.SmtpClient SMTP 协议
即 Host 处理事务的主机或IP地址 //smtp.163.com
UseDefaultCredentials 使用默认的证书 //false
DeliveryMethod 指定如何处理代发的电子邮件 //SmtpDeliveryMethod.Network 电子邮件通过网络发到SMTP
Credentials 设置代发人凭据 //new System.Net.NetworkCredential("用户名","密码")
2.MailMessage 即Email发送消息 (添加到SmtpClient中)
Subject 主题
Body 邮件正文
BodyEncoding 正文编码 //Encoding.UTF8;
IsBodyHtml是否是HTML 格式 //true
Priority 电子邮件优先级 // MailPriority.High;
3.Attachment 电子邮件附件 (添加到MailMessage中)
Attachment attach = new Attachment("文件路径",System.Net.Mime.MediaTypeNames.Application.Octet);
整体例子:
MailMessage message = new MailMessage(famail, dsUserInfo.Tables[0].Rows[j]["Email"].ToString());
message.Subject = title;
message.Body = content;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
if (!string.IsNullOrEmpty(dsUserInfo.Tables[0].Rows[j]["Attachment"].ToString()))
{
Attachment attach = new Attachment(dsUserInfo.Tables[0].Rows[j]["Attachment"].ToString(), System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(attach);
}
try
{
client.Send(message);
listBox1.Items.Add("" + dsUserInfo.Tables[0].Rows[j]["SentName"].ToString() + " 的邮件发送成功!");
}
catch (Exception ex)
{
listBox1.Items.Add("第" + (j + 1) + "失败!" + ex.ToString());
}