一个发邮件的类(用CDO实现)

此博客给出了Java实现发送普通文本邮件的代码示例。定义了SendMail类,包含发送邮件的方法,设置了发信人、收信人、主题、正文等信息,还配置了SMTP服务器、认证方式等,若发送异常会抛出异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class SendMail
{
public SendMail()
{

}
//发送普通文本Email方法入口
//From:发信人邮件地址 To:收信人邮件地址
// FromName:发信人名称 ToName:收信人名称
//Subject:信件主题
//Body:邮件正文
public int Send(string strTitle,string strTo,string strContent)
{
try
{
CDO.Message oMsg = new CDO.Message();

oMsg.From = "Administrator@innosis.com.cn";
oMsg.To = strTo;//"AtenShen@innosis.com.cn";
oMsg.Subject =strTitle;// "MailTest";

oMsg.HTMLBody = "<html><body>s"+strContent+"</body></html>";

CDO.IConfiguration iConfg = oMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;

oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value="JamesWang"; //sender mail
oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value="JamesWang"; //email account
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="JamesWang";
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="531713Wt";
oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
//value=0 代表Anonymous验证方式(不需要验证)
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
//The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="192.168.161.32";

oFields.Update();
oMsg.BodyPart.Charset="gb2312";
oMsg.HTMLBodyPart.Charset="gb2312";

oMsg.Send();
oMsg = null;
}
catch (Exception e)
{
return -1;
throw e;
}
return 0;

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值