C# 用c在asp.net中发送邮件脚本#
请给我推荐一个asp.net中带有C的发送邮件脚本的代码。我想建立查询表单,在那里我想发送我的电子邮件Id上的所有信息,包括附件、主题和正文。这表明我正在这样做。它应该涵盖你需要的一切 这也显示了如何添加附件,如果您需要的话。C# 用c在asp.net中发送邮件脚本#,c#,asp.net,C#,Asp.net,请给我推荐一个asp.net中带有C的发送邮件脚本的代码。我想建立查询表单,在那里我想发送我的电子邮件Id上的所有信息,包括附件、主题和正文。这表明我正在这样做。它应该涵盖你需要的一切 这也显示了如何添加附件,如果您需要的话。试试看 try { // Assign a sender, recipient and subject to new mail message MailAddress sender = new MailAddress("sender@john
试试看
try
{
// Assign a sender, recipient and subject to new mail message
MailAddress sender =
new MailAddress("sender@johnnycoder.com", "Sender");
MailAddress recipient =
new MailAddress("recipient@johnnycoder.com", "Recipient");
MailMessage m = new MailMessage(sender, recipient);
m.Subject = "Test Message";
// Define the plain text alternate view and add to message
string plainTextBody =
"You must use an email client that supports HTML messages";
AlternateView plainTextView =
AlternateView.CreateAlternateViewFromString(
plainTextBody, null, MediaTypeNames.Text.Plain);
m.AlternateViews.Add(plainTextView);
// Define the html alternate view with embedded image and
// add to message. To reference images attached as linked
// resources from your HTML message body, use "cid:contentID"
// in the <img> tag...
string htmlBody =
"<html><body><h1>Picture</h1><br>" +
"<img src=\"cid:SampleImage\"></body></html>";
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(
htmlBody, null, MediaTypeNames.Text.Html);
// ...and then define the actual LinkedResource matching the
// ContentID property as found in the image tag. In this case,
// the HTML message includes the tag
// <img src=\"cid:SampleImage\"> and the following
// LinkedResource.ContentId is set to "SampleImage"
LinkedResource sampleImage =
new LinkedResource("sample.jpg",
MediaTypeNames.Image.Jpeg);
sampleImage.ContentId = "SampleImage";
htmlView.LinkedResources.Add(sampleImage);
m.AlternateViews.Add(htmlView);
// Finally, configure smtp or alternatively use the
// system.net mailSettings
SmtpClient smtp = new SmtpClient
{
Host = "smtp.bigcompany.com",
UseDefaultCredentials = false,
Credentials =
new NetworkCredential("username", "password")
};
//<system.net>
// <mailSettings>
// <smtp deliveryMethod="Network">
// <network host="smtp.bigcompany.com"
// port="25" defaultCredentials="true"/>
// </smtp>
// </mailSettings>
//</system.net>
smtp.Send(m);
}
catch (ArgumentException)
{
throw new
ArgumentException("Undefined sender and/or recipient.");
}
catch (FormatException)
{
throw new
FormatException("Invalid sender and/or recipient.");
}
catch (InvalidOperationException)
{
throw new
InvalidOperationException("Undefined SMTP server.");
}
catch (SmtpFailedRecipientException)
{
throw new SmtpFailedRecipientException(
"The mail server says that there is no mailbox for recipient");
}
catch (SmtpException ex)
{
// Invalid hostnames result in a WebException InnerException that
// provides a more descriptive error, so get the base exception
Exception inner = ex.GetBaseException();
throw new SmtpException("Could not send message: " + inner.Message);
}
{
//为新邮件指定发件人、收件人和主题
邮寄地址发送者=
新邮件地址(“sender@johnnycoder.com“,”发送方“);
邮寄地址收件人=
新邮件地址(“recipient@johnnycoder.com“,”收件人“);
MailMessage m=新邮件消息(发件人、收件人);
m、 主题=“测试消息”;
//定义纯文本替代视图并添加到消息
字符串明文体=
“您必须使用支持HTML消息的电子邮件客户端”;
AlternateView纯文本视图=
AlternateView.CreateAlternateView-FromString(
plainTextBody,null,MediaTypeNames.Text.Plain);
m、 AlternateViews.Add(纯文本视图);
//使用嵌入的图像和
//添加到邮件。引用作为链接附加的图像
//在HTML消息正文中的资源中,使用“cid:contentID”
//在"中,;
备选视图htmlView=
AlternateView.CreateAlternateView-FromString(
htmlBody,null,MediaTypeNames.Text.Html);
//…然后定义与
//在图像标记中找到的ContentID属性。在本例中,
//HTML消息包含标记
//以及以下
//LinkedResource.ContentId设置为“SampleImage”
LinkedResource样本图像=
新建LinkedResource(“sample.jpg”,
MediaTypeNames.Image.Jpeg);
sampleImage.ContentId=“sampleImage”;
添加(sampleImage);
m、 alternativeviews.Add(htmlView);
//最后,配置smtp或使用
//system.net邮件设置
SmtpClient smtp=新SmtpClient
{
Host=“smtp.bigcompany.com”,
UseDefaultCredentials=false,
证书=
新网络凭据(“用户名”、“密码”)
};
//
//
//
//
//
//
//
smtp.Send(m);
}
捕获(异常)
{
抛出新的
ArgumentException(“未定义的发件人和/或收件人”);
}
捕获(格式化异常)
{
抛出新的
格式异常(“无效的发件人和/或收件人”);
}
捕获(无效操作异常)
{
抛出新的
InvalidOperationException(“未定义的SMTP服务器”);
}
捕获(SmtpFailedRecipientException)
{
抛出新的SmtpFailedRecipientException(
“邮件服务器说收件人没有邮箱”);
}
捕获(SMTPEx异常)
{
//无效的主机名导致WebException InnerException
//提供更具描述性的错误,因此获取基本异常
异常内部=例如GetBaseException();
抛出新的SmtpException(“无法发送消息:+inner.message”);
}
无耻插头:
是的,这是我的网站,但它涵盖了你需要用System.Net.Mail做的99%的事情