//同樣引入
using System.Net.Mail;
//在這裡我們在page_load時就發郵件
protected void Page_Load(object sender, EventArgs e){
string strAllEmail = "sdf,sdf,sdf,";//this.txtAllEmail.Text;
string[] strEachEmail = strAllEmail.Split(',');
int i_max=strEachEmail.Length;
int i;
for (i = 0; i < i_max;i++ )
SendEmail(strEachEmail[i].ToString());
}

//要調用的發送函數
private SendEmail(string astrEachEmail)
{
//发送
string strSendEmail = "發送者郵件地址";
string strUserName = "申請人用戶名";
string strPassWord = "申請人密碼";
string strRecieveEMail = astrEachEmail;//"接收者郵件地址"
string strSubject = "主題";
string strBody = "內容";
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(strRecieveEMail);
////附件處理
//if (txtAttachment.Text.Trim().Length > 0)
//{
// string FileName = @txtAttachment.Text.Trim();
// System.Net.Mail.Attachment pAttachment = new Attachment(FileName);
// msg.Attachments.Add(pAttachment);
//}
msg.From = new MailAddress(strSendEmail) ;

msg.Subject = strSubject;//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = strBody;//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级

SmtpClient client = new SmtpClient();
client.Host = txtSmtp.Text.Trim();//为邮件服务器设置
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(strUserName, strPassWord);
client.Credentials = basicAuthenticationInfo;

object userState = msg;
try
{
client.Send(msg);
//client.SendAsync(msg, userState);//简单一点儿可以client.Send(msg);
Response.Write("成功发送到" + strRecieveEMail + "邮箱");
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write("发送邮件" + strRecieveEMail + "出错");
throw ex;
}
}

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐