string _toEmail,_subject,_body,_fromEmail,_fromPassword;
_toEmail = "goot2012@live.com";//获取收件人邮箱
_subject = "你好,我是";//获取邮件主题
_body = "你好! 你吃了饭没!";//获取邮件正文
_fromEmail = "xxxxxx@qq.com";//获取发件人邮箱
_fromPassword = "xxxxxxxxx";//获取发件人密码
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(_fromEmail, _fromPassword);//设置发件人的邮箱和密码
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//以SMTP形式发送邮件
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(_toEmail);//设置收件人的邮箱
// message.To.Add("503164193@qq.com");
message.From = new System.Net.Mail.MailAddress(_fromEmail);//设置发件人的邮箱和别名
message.Subject = _subject;//设置邮件主题
message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8");//主题编码
message.Body = _body;//设置邮件正文
message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");//正文编码
message.IsBodyHtml = true;//是否以HTML发送
//附件
string strFilePath = @"E:\logo.jpg";
System.Net.Mail.Attachment p_w_upload1 = new System.Net.Mail.Attachment(strFilePath);//添加附件
p_w_upload1.Name = System.IO.Path.GetFileName(strFilePath);
p_w_upload1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312");
p_w_upload1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
p_w_upload1.ContentDisposition.Inline = true;
p_w_upload1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = p_w_upload1.ContentId;//关键性的地方,这里得到一个id数值
message.Attachments.Add(p_w_upload1);
//string name = System.IO.Path.GetFileName(FileUpload1.FileName);
//if (string.IsNullOrEmpty(name) && name != "")
// message.Attachments.Add(new System.Net.Mail.Attachment(name));
try{
client.Send(message);//发送邮件
}
catch(Exception e)
{=-
MessageBox.Show("邮件发送失败!{0}",e.Message);
}
//if(System.IO.File.Exists(name))
//System.IO.File.Delete(name);