c# - 发送电子邮件的正确代码,asp.net

标签 c# asp.net smtp

我有一个表单,允许用户向邮件列表(linq 表)上的每个人发送电子邮件。我在链接到 smtp 服务器的正确代码和语法方面遇到问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Web.Security;
using System.Web.Mail;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
using System.Net.Mail;



public partial class MassEmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        mailingListClassDataContext Class = new mailingListClassDataContext();
        var emaillist = from emails in Class.mailinglistMembers select emails.email;

        foreach (var subcriber in emaillist)
        {


                MailMessage objMail = new MailMessage();
                objMail.From = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6d2c3d5d2e6d2c3d5d288c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a>";

                objMail.To = subcriber;

                objMail.BodyFormat = MailFormat.Html ;


                //The subject of the message 
                objMail.Subject = "test email that i hope works" ;

                //he message text 
                objMail.Body = Editor1.Content;

                //need help in this area
                SmtpClient client = new SmtpClient();

                SmtpClient.Send(objMail);

                }
            }
}

最佳答案

最好的解决方案是将 smtp 服务器详细信息放入您的 web.config

    <system.net>
        <mailSettings>
            <smtp>
                <network
                   host="smtp.emailhost.com"
                   port="25"
                   userName="username"
                   password="password" />
            </smtp>
        </mailSettings>
    </system.net>
  <system.web>

关于c# - 发送电子邮件的正确代码,asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3881156/

相关文章:

c# - 如何本地化 AppBar 按钮

c# - .csproj 文件 - 程序化添加/删除文件

c# - 将 IN 运算符与存储过程参数一起使用

asp.net - ASP 中的全局化将语言法语、乌尔都语、英语更改为下拉列表

c# - 如何在邮件正文中包含链接?

c# - 为什么.NET 在某些类中使用 int 而不是 uint?

c# - 解析日期时间

c# - 无法在接口(interface)方法中创建抽象类的实例

java - 无法使用 xoauth 在 javamail 上向 gmail 发送电子邮件

JavaMail - 发送经过身份验证的 oauth2 smtp 邮件