【问题标题】:sending mail by System.Net.Mail通过 System.Net.Mail 发送邮件
【发布时间】:2011-10-21 22:51:00
【问题描述】:

我是 ASP.Net 篮子里刚出生的婴儿。我正在阅读 Wrox Beginners for ASP.NET 4.0 并且正在阅读有关使用 System.Net.Mail 发送邮件的信息。

protected void Page_Load(object sender, EventArgs e)
{
    MailMessage myMessage = new MailMessage();  // Getting Error
    myMessage.Subject = "Test Message";
    myMessage.Body = "Hello World, from Planet Wrox";
    myMessage.From = new MailAddress("sendID@net.com", "Barack Obama");
    myMessage.To.Add(new MailAddress("recieveID@net.com", "George Bush"));
    SmtpClient mySmtpClient = new SmtpClient();
    mySmtpClient.Send(myMessage);
}

我在页面加载事件中编写了这段代码。但我在第一行遇到错误,因为

“/”应用程序中的服务器错误。

指定的字符串不在 电子邮件地址所需的表格。描述:一个未处理的 执行当前 Web 请求期间发生异常。 请查看堆栈跟踪以获取有关错误的更多信息和 它起源于代码。

异常详细信息:System.FormatException:指定的字符串不是 以电子邮件地址所需的形式。

来源错误:

Line 11:     protected void Page_Load(object sender, EventArgs e)
Line 12:     { 
Line 13:         MailMessage myMessage = new MailMessage();
Line 14:         myMessage.Subject = "Test Message"; 
Line 15:         myMessage.Body = "Hello World, from Planet Wrox";

谁能告诉我是什么问题?

【问题讨论】:

  • 对我宣传这种行为感到羞耻,但对“我是 ASP.Net 篮子里刚出生的婴儿”这句话 +1。太搞笑了!

标签: asp.net email


【解决方案1】:

这是一个包含你需要的一切的课程:

/// <summary>
/// Wrapper class for the System.Net.Mail objects
/// </summary>
public class SmtpMailMessage : IDisposable
{
    #region declarations

    MailMessage Message;
    SmtpClient SmtpMailClient;

    #endregion

    #region constructors

    /// <summary>
    /// Default constructor for the SmtpMailMessage class
    /// </summary>
    public SmtpMailMessage()
    {
        //initialize the mail message
        Message = new MailMessage();
        Message.Priority = MailPriority.Normal;
        Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;            
        Message.From = new MailAddress("abc@abc.com");           

        //initialize the smtp client
        SmtpMailClient = new SmtpClient();
        SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        SmtpMailClient.Host = "192.168.0.1";
        SmtpMailClient.Port = 25;
    }

    /// <summary>
    /// Parameterized constructor for the SmtpMailMessage class. Allows for override of the default
    /// SMTP host and port number
    /// </summary>
    /// <param name="HostIP">The IP address of the exchange server</param>
    /// <param name="PortNumber">The port number for ingoing and outgoing SMTP messages</param>
    public SmtpMailMessage(string HostIP, int PortNumber) : this()
    {
        //override the smtp host value
        SmtpMailClient.Host = HostIP;

        //override the smtp port value
        SmtpMailClient.Port = PortNumber;
    }

    #endregion

    #region subject / body

    /// <summary>
    /// The body content of the mail message
    /// </summary>
    public string Body
    {
        get
        {
            return Message.Body;
        }
        set
        {
            Message.Body = value;
        }
    }

    /// <summary>
    /// the subject of the mail message
    /// </summary>
    public string Subject
    {
        get
        {
            return Message.Subject;
        }
        set
        {
            Message.Subject = value;
        }
    }

    #endregion

    #region mail type

    /// <summary>
    /// Gets or sets a value that determines whether the mail message
    /// should be formatted as HTML or text
    /// </summary>
    public bool IsHtmlMessage
    {
        get
        {
            return Message.IsBodyHtml;
        }
        set
        {
            Message.IsBodyHtml = value;
        }
    }

    #endregion

    #region sender

    /// <summary>
    /// Gets or sets the from address of this message
    /// </summary>
    public string From
    {
        get
        {
            return Message.From.Address;
        }
        set
        {
            Message.From = new MailAddress(value);
        }
    }

    #endregion

    #region recipients

    /// <summary>
    /// Gets the collection of recipients
    /// </summary>
    public MailAddressCollection To
    {
        get
        {
            return Message.To;                
        }
    }

    /// <summary>
    /// Gets the collection of CC recipients 
    /// </summary>
    public MailAddressCollection CC
    {
        get
        {
            return Message.CC;
        }
    }

    /// <summary>
    /// Gets the collection of Bcc recipients
    /// </summary>
    public MailAddressCollection Bcc
    {
        get
        {
            return Message.Bcc;
        }
    }

    #endregion

    #region delivery notification

    /// <summary>
    /// Gets or sets the delivery notification settings for this message
    /// </summary>
    public DeliveryNotificationOptions DeliveryNotifications
    {
        get
        {
            return Message.DeliveryNotificationOptions;
        }
        set
        {
            Message.DeliveryNotificationOptions = value;
        }
    }

    #endregion

    #region priority

    /// <summary>
    /// Gets or sets the Priority of this message
    /// </summary>
    public MailPriority PriorityLevel
    {
        get
        {
            return Message.Priority;
        }
        set
        {
            Message.Priority = value;
        }
    }

    #endregion

    #region send methods

    /// <summary>
    /// Sends the message anonymously (without credentials)
    /// </summary>
    public void Send()
    {
        SmtpMailClient.Send(Message);
    }

    /// <summary>
    /// Sends the message with authorization from a network account   
    /// </summary>
    /// <param name="Username">The Windows username of the authorizing user</param>
    /// <param name="Password">The Windows password of the authorizing user</param>
    /// <param name="Domain">The domain name of the network to which the authorizing user belongs</param>
    public void Send(string Username, string Password, string Domain)
    {
        //attach a network credential to this message using the information passed into the method
        SmtpMailClient.Credentials = new NetworkCredential(Username, Password, Domain);

        //send the message
        SmtpMailClient.Send(Message);
    }

    #endregion

    #region IDisposable implementation

    ~SmtpMailMessage()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);            
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (Message != null)
                Message.Dispose();
            Message = null;                
            SmtpMailClient = null;
        }
    }

    #endregion        
}

【讨论】:

    【解决方案2】:

    我相信,为了让您使用带有空构造函数的 MailMessage(),您需要在配置文件的 SmtpSettings 中设置“From”值。从这里开始:

    http://msdn.microsoft.com/en-us/library/ms144707.aspx

    备注


    From 设置为网络元素中的值 mailSettings 元素(网络设置),如果存在。

    一个更简单的解决方案可能是使用带有 From 地址和 To 地址的构造函数。

    从这里: http://msdn.microsoft.com/en-us/library/14k9fb7t.aspx

    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    MailMessage message = new MailMessage(from, to);
    

    【讨论】:

      【解决方案3】:

      请检查 web.config 文件中的 smtp 设置是否正确。

      【讨论】:

        猜你喜欢
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-08
        • 2018-04-13
        • 2013-04-02
        • 2012-08-20
        相关资源
        最近更新 更多