日期:2014-05-17 浏览次数:20377 次
MailAddress from = new MailAddress("070812041@163.com", "东东"); //邮件的发件人,后面为显示的名字 MailMessage mail = new MailMessage(); mail.Subject = txtSubject.Text; //设置邮件的标题 mail.From = from; //设置邮件的发件人 //多人发送 string address = ""; string displayName = ""; string[] mailNames = (txtMailTo.Text + ";").Split(';'); foreach (string name in mailNames) { if (name != string.Empty) { if (name.IndexOf('<') > 0) { displayName = name.Substring(0, name.IndexOf('<')); address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' '); } else { displayName = string.Empty; address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' '); } // mail.To.Add(new MailAddress(address, displayName)); mail.CC.Add(new MailAddress(address, displayName)); //收件人地址的集合 } } mail.Body = txtBody.Text; //设置邮件的内容 mail.BodyEncoding = System.Text.Encoding.UTF8; //设置邮件的格式 mail.IsBodyHtml = true; //设置正文是否为HTML格式 mail.Priority = MailPriority.Normal; //设置邮件的发送级别 if (txtMailTo.Text != "") { string fileName = txtUpFile.Text.Trim(); //设置邮件的附件 fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); //取文件名 mail.Attachments.Add(new Attachment(fileName)); //添加附件到邮件当中 } mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; SmtpClient client = new SmtpClient(); client.Host = "smtp.163.com"; //设置SMTP的地址,注意:是什么邮箱就应该用相对应的地址 client.Port = 25; //设置用于 SMTP 事务的端口,默认的是 25 client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("*****", "*****");//我的邮箱的登录名和密码。就是发送方的用户名和密码,要对应上面的Host地址 client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Send(mail); //邮件的正式发送 MessageBox.Show("发送成功"); } private void btn_send_Click(object sender, EventArgs e) { sendMail(); }
------解决方案--------------------
图片也可作为附件发送
MailMessage message = new MailMessage(From, To, Subject, Body);
Attachment data = null;
if (Files != null && Files.Length > 0)
{
foreach (string file in Files)