62,242
社区成员




public bool SendReady(string fromEmail, string fromPsaaWord, string toEmail, string subject, string body, string[] attachmentsPath)
{
bool Success = true;
//验证合法邮箱的表达式,正确时返回true
if (!Regex.IsMatch(fromEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
Success = false;
//邮箱不合法
MessageBox("邮箱不合法");
}
if (!Regex.IsMatch(toEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
Success = false;
MessageBox("邮箱不合法");
}
if (subject.Trim() == "")
{
MessageBox("请填写主题");
Success = false;
}
if (body.Trim() == "")
{
MessageBox("请填写内容");
Success = false;
}
else
{
MailAddress from = new MailAddress(fromEmail);
//收件人地址
MailAddress to = new MailAddress(toEmail);
//邮件主题、内容
MailMessage message = new MailMessage(from, to);
//主题
message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
//内容
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
//在有附件的情况下添加附件
try
{
if (attachmentsPath != null && attachmentsPath.Length > 0)
{
Attachment attachFile = null;
foreach (string path in attachmentsPath)
{
attachFile = new Attachment(path);
message.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
// MessageBox("在添加附件时有错误");
Success = false;
throw new Exception("在添加附件时有错误:" + err);
}
try
{
//大部分邮件服务器均加smtp.前缀
SmtpClient client = new SmtpClient("smtp." + from.Host);
client.Port = 465;
//不使用默认凭证,注意此句必须放在client.Credentials的上面
client.UseDefaultCredentials = false;
//指定用户名、密码
client.Credentials = new NetworkCredential(from.Address, fromPsaaWord);
//邮件通过网络发送到服务器
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(message);
}
catch (Exception e)
{
Success = false;
throw new Exception(e.ToString());
}
finally
{
//及时释放占用的资源
message.Dispose();
}
}
catch (SmtpException err)
{
Success = false;
//如果错误原因是没有找到服务器,则尝试不加smtp.前缀的服务器
if (err.StatusCode == SmtpStatusCode.GeneralFailure)
{
try
{
//有些邮件服务器不加smtp.前缀
SmtpClient client = new SmtpClient(from.Host);
client.Port = 25;
//不使用默认凭证,注意此句必须放在client.Credentials的上面
client.UseDefaultCredentials = false;
//指定用户名、密码
client.Credentials = new NetworkCredential(from.Address, fromPsaaWord);
//邮件通过网络发送到服务器
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
}
catch (SmtpException)
{ }
}
else
{ }
}
}
return Success;
}
//建立一个类sendEmail.cs
代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net.Mail;
using System.Net;
/// <summary>
///sendEmail 的摘要说明
/// </summary>
public class sendEmail
{
public sendEmail()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 向用户发送邮件
/// </summary>
/// <param name="receiveUser">接收邮件的用户</param>
/// <param name="sendUser">发送者</param>
/// <param name="sendusername">发送者的邮箱登陆名</param>
/// <param name="userpassword">发送者的登陆密码</param>
/// <param name="sendTitle">发送标题</param>
/// <param name="sendContent">发送的内容</param>
/// <returns></returns>
public void sendMail(string receiveUser, string sendUser, string sendusername, string userpassword, string sendTitle, string sendContent)
{
MailAddress to = new MailAddress(receiveUser);//接收者邮箱
MailAddress from = new MailAddress(sendUser);//发送者邮箱
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
//mail.To.Add("xx@xx.com");//添加接收者
mail.Subject = sendTitle;
mail.IsBodyHtml = true;
mail.Body = sendContent;
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com";//设置发送者邮箱对应的smtpserver
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(sendusername, userpassword);//设置发送者邮箱对应的username和password
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
}
}
//btn事件
sendEmail se = new sendEmail();
// se.sendMail("2854762@qq.com","wj","zhao88","9523","您好测试","测试中");
se.sendMail("2854762@qq.com", "lie_a@163.com", "lie_a", "8088", "千机购物重设密码信", "aa");
System.Windows.Forms.MessageBox.Show("发送成功");
//引用空间名:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net.Mail;
using System.Net;