发送邮箱需要开通pop3/smtp服务,否则qq邮箱,网易邮箱等会报错。
public static class emailhelper
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name=”subject”>邮件主题</param>
/// <param name=”msg”>邮件内容</param>
/// <param name=”filepath”>附件地址,如果不添加附件传null或””</param>
/// <param name=”senderemail”>发送人邮箱地址</param>
/// <param name=”senderpwd”>发送人邮箱密码</param>
/// <param name=”recipientemail”>接收人邮箱</param>
public static void sendmail(string subject, string msg, string filepath, string senderemail, string senderpwd, params string[] recipientemail)
{
if (!checkisnotemptyornull(subject, msg, senderemail, senderpwd) || recipientemail == null || recipientemail.length == 0)
{
throw new exception(“输入信息无效”);
}
try
{
string[] sendfromuser = senderemail.split(\\\’@\\\’);
//构造一个email的message对象
mailmessage message = new mailmessage();
//确定smtp服务器地址。实例化一个smtp客户端
system.net.mail.smtpclient client = new system.net.mail.smtpclient(“smtp.” sendfromuser[1]);
//构造发件人地址对象
message.from = new mailaddress(senderemail, sendfromuser[0], encoding.utf8);
//构造收件人地址对象
foreach (string username in recipientemail)
{
message.to.add(new mailaddress(username, username.split(\\\’@\\\’)[0], encoding.utf8));
}
if (!string.isnullorempty(filepath))
{
attachment attach = new attachment(filepath);
//得到文件的信息
contentdisposition disposition = attach.contentdisposition;
disposition.creationdate = system.io.file.getcreationtime(filepath);
disposition.modificationdate = system.io.file.getlastwritetime(filepath);
disposition.readdate = system.io.file.getlastaccesstime(filepath);
//向邮件添加附件
message.attachments.add(attach);
}
//添加邮件主题和内容
message.subject = subject;
message.subjectencoding = encoding.utf8;
message.body = msg;
message.bodyencoding = encoding.utf8;
//设置邮件的信息
client.deliverymethod = smtpdeliverymethod.network;
message.bodyencoding = system.text.encoding.utf8;
message.isbodyhtml = false;
//如果服务器支持安全连接,则将安全连接设为true。
//gmail,qq支持,163不支持
switch (sendfromuser[1])
{
case “gmail.com”:
case “qq.com”:
client.enablessl = true;
break;
default:
client.enablessl = false;
break;
}
//设置用户名和密码。
client.usedefaultcredentials = false;
//用户登陆信息
networkcredential mycredentials = new networkcredential(senderemail, senderpwd);
client.credentials = mycredentials;
//发送邮件
client.send(message);
}
catch (exception ex)
{
throw (ex);
}
}
/// <summary>
/// 验证所有传入字符串不能为空或null
/// </summary>
/// <param name=”ps”>参数列表</param>
/// <returns>都不为空或null返回true,否则返回false</returns>
public static bool checkisnotemptyornull(params string[] ps)
{
if (ps != null)
{
foreach (string item in ps)
{
if (string.isnullorempty(item)) return false;
}
return true;
}
return false;
}
}
如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。
西部数码是专业企业邮箱的官方正规提供商,21年行业经验,提供安全稳定,简单易用,高性价比的企业邮箱,按需自由定制,不限空间,极速收发,能够满足用户对企业邮箱的不同需求。可以通过以下几种方式注册、申请、购买、试用、开通企业邮箱:
1、登录https://www.west.cn/services/mail/在线咨询申请试用或购买;
2、直接致电028-62778877申请试用或正式购买开通;
云服务器怎么过白名单OIF:开源协作驱动下一个10年 构建全球化的无界创新提升需要搜集用户意见反馈来提升seo优化效果吗?应该买什么样的云服务器好空间今天不稳定-虚拟主机/数据库问题云服务器ecs租用多少费用公司没有座机号麻烦换成紧急联系人谢谢域名交易真的赚钱吗?