发送电子邮件通知 [英] sending email notifications

查看:101
本文介绍了发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从网络应用程序项目发送电子邮件通知?

how to send email notifications from web application projects?

推荐答案

电子邮件的发送将在服务器部分完成,类似于使用
The sending of the emails will be done on the server part and is similar with the sending emails from desktop or service applications by using methods from
System.Net.Mail

库中的方法从桌面或服务应用程序发送电子邮件。应该与下一个源代码类似:



public static void SendEmailMessage(string smtpHost,string smtpEmail,string fromEmailAddress,string toEmailAddress,string subject,string message,string attachmentFile = null)

{

试试

{

string [] temp = smtpEmail.Split(new char [] {'';''},StringSplitOptions.RemoveEmptyEntries);

//

//创建并初始化MailMessage对象。

//

if(fromEmailAddress == null)

fromEmailAddress = temp [0];

//

MailMessage mailMessage = new MailMessage(fromEmailAddress,toEmailAddress,subject,message);

mailMessage.BodyEncoding = System.Text.UnicodeEncoding.Default;

mailMessage.IsBodyHtml = t rue;

if(attachmentFile!= null)

mailMessage.Attachments.Add(new Attachment(attachmentFile));

//

//创建并初始化SMTP客户端

//

string [] smtpHostData = smtpHost.Split('';'');

int port = 25; // 587 - 适用于Excange服务器!

if(smtpHostData.Length> 1)

port = int.Parse(smtpHostData [1]);

//

SmtpClient smtpClient =新的SmtpClient(smtpHostData [0],端口);

smtpClient.UseDefaultCredentials = false;

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //!?

//

if(temp.Length> 1)

smtpClient.Credentials = new System.Net.NetworkCredential( temp [0],temp [1]);

//

//发送电子邮件。

//

smtpClient.Send(mailMessage);

}

catch(SmtpFailedRecipientException ex)

{

LogicEventLog .LogException(ex);

抛出新的SmtpFailedRecipientException(toEmailAddress);

}

}

library. Should be something similar with the next source code:

public static void SendEmailMessage(string smtpHost, string smtpEmail, string fromEmailAddress, string toEmailAddress, string subject, string message, string attachmentFile = null)
{
try
{
string[] temp = smtpEmail.Split(new char[]{'';''}, StringSplitOptions.RemoveEmptyEntries);
//
// Create and init the MailMessage object.
//
if (fromEmailAddress == null)
fromEmailAddress = temp[0];
//
MailMessage mailMessage = new MailMessage(fromEmailAddress, toEmailAddress, subject, message);
mailMessage.BodyEncoding = System.Text.UnicodeEncoding.Default;
mailMessage.IsBodyHtml = true;
if (attachmentFile != null)
mailMessage.Attachments.Add(new Attachment(attachmentFile));
//
// Create and init the SMTP Client
//
string[] smtpHostData = smtpHost.Split('';'');
int port = 25; // 587 - is for Excange server!
if (smtpHostData.Length > 1)
port = int.Parse(smtpHostData[1]);
//
SmtpClient smtpClient = new SmtpClient(smtpHostData[0], port);
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //!?
//
if (temp.Length > 1)
smtpClient.Credentials = new System.Net.NetworkCredential(temp[0], temp[1]);
//
// Send the email.
//
smtpClient.Send(mailMessage);
}
catch (SmtpFailedRecipientException ex)
{
LogicEventLog.LogException(ex);
throw new SmtpFailedRecipientException(toEmailAddress);
}
}


这篇关于发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆