VBScriptC++/CLIWindows 2003WebFormsWindows 2000Windows XP初学者开发Visual StudioJavascriptWindows.NETVisual BasicASP.NET
(ASP.NET)使用 web.config 文件中的 IP 设置,以 HTML 格式通过 SMTP 发送邮件






1.79/5 (11投票s)
2005年11月7日
1分钟阅读

115435

1398
本文简要介绍了使用 SMTP 以 HTML 格式发送邮件,该邮件从 web.config 文件获取默认 SMTP 设置。
引言
本文介绍了如何使用 SMTP 邮件服务器以 HTML 格式发送邮件。它使用 web.config 中的默认设置来发送电子邮件。
源代码
如何在 web.config 中设置值
使用 VS.NET 编辑器或任何其他文本编辑器打开 web.config 文件。
在 <appSettings> 标签之间添加以下代码。添加以下内容并使用您的 IP </appSettings> 标签。您的最终代码如下所示
<appSettings> <add key="SmtpServer" value="<Your SMTP IP>"/> </appSettings>
将代码添加到表单
在 ASP.NET 应用程序中添加一个名为 SendClick 的按钮。
将以下代码粘贴到按钮单击事件中
Private Sub SendClick() Dim mailMsg As New MailMessage Try mailMsg.To = "<tomailid>" mailMsg.From = "<frommailid>" 'mailMsg.BodyFormat = MailFormat.Text 'optional for sending text in body mailMsg.BodyFormat = MailFormat.Html mailMsg.Subject = "Statistics Report" mailMsg.Body = "<html><body><Table><tr><td>Hi,</td></tr><tr><td>Details of the Statistics :</td></tr></Table></body></html><html><body>" & "sometext" & _ "</body></html><html><body><Table><tr><td> </td></tr><tr><td>NOTE: This is an automated mail. Please, do not reply.</td></tr>" & _ "<tr><td>*Green coloured rows indicates temporary demos</td></tr>" & _ "<tr><td>**All statistics are based on the page naming conventions Eg., 22_10_2005_</td></tr>" & _ "<tr><td> </td></tr><tr><td>Regards,</td></tr><tr><td>some text,</td></tr><tr><td>some text,</td></tr>" & _ "<tr><td> Some text </td></tr></table></body></html>" SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SmtpServer") SmtpMail.Send(mailMsg) 'xm.InnerHtml = "Your message has been sent" Catch ex As Exception Response.Write(ex.Message) End Try End Sub
在上面的代码中,'mailMsg' 是类型为 MailMessage 的对象的名称。我们使用此对象来设置“收件人”地址、“发件人”地址、“BodyFormat”等。
我们将 body format 标签设置为“MailFormat.HTML”,因为 body format 的类型为 HTML。我们可以使用“MailFormat.Text”来设置文本格式。“主题”用于设置主题以及“抄送”和“密送”。
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SmtpServer")
在上面的行中,我们将 IP 设置为对象。
Configurations.AppSettings("SmtpServer") 将从 web.config 读取值并动态分配它。
SmtpMail.Send(mailMsg)上面的代码行将发送邮件......
祝您发送邮件愉快..... :)