-5
A
回答
2
快速修復與示例代碼,請參閱http://www.orcsweb.com/blog/brad/sending-email-from-asp-net-4-c-sample-code/
對於更深入,見http://www.asp.net/general/videos/how-do-i-use-aspnet-to-send-email-from-a-web-site
+0
第一個鏈接中的代碼不安全。 MailMessage實現IDisposable,但代碼不會以任何方式處理消息。使用IDisposable的地方應該包括在內。 –
1
您需要使用System.Net.Mail,和該SmtpClient MAILMESSAGE和對象,像這樣:
using (MailMessage mail = new MailMessage("[email protected]", "[email protected]"))
{
//supply host name and mail port
SmtpClient client = new SmtpClient("YourHostName", 25);
mail.Subject = "Hello";
mail.Body = "Mail body";
mail.IsBodyHtml = true;
client.Send(mail);
}
這裏的一類我創建的encompa sses以上:
/// <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("[email protected]");
//initialize the smtp client
SmtpMailClient = new SmtpClient();
SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpMailClient.Host = "192.168.0.21";
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
}
相關問題
- 1. 從localhost ASP.net發送電子郵件
- 2. 定期從Asp.Net發送電子郵件
- 3. 如何從ASP.NET的* any *電子郵件地址發送電子郵件?
- 4. 如何從PHPmyAdmin發送電子郵件?
- 5. 如何從Node.js發送電子郵件?
- 6. 如何從C發送電子郵件
- 7. 發送電子郵件從
- 8. ASP.NET MVC發送電子郵件附件
- 9. 如何在asp.net中通過電子郵件發送郵件
- 10. asp.net的MVC發送電子郵件錯誤:無法發送電子郵件
- 11. 在asp.net發送電子郵件發送密碼而不是電子郵件
- 12. 如何從Asp.net Mvc-3發送電子郵件?
- 13. 你如何從ASP.NET發送大量電子郵件?
- 14. 發送電子郵件提醒與ASP.NET
- 15. 發送電子郵件asp.net c#
- 16. 使用ASP.NET發送電子郵件
- 17. 通過asp.net發送電子郵件3.5
- 18. 在asp.net中發送電子郵件
- 19. ASP.net 3.5發送網頁電子郵件
- 20. 用ASP.Net發送海量電子郵件
- 21. 在asp.net發送大量電子郵件
- 22. PHP發送電子郵件多次發送電子郵件
- 23. 如何通過電子郵件發送電子郵件地址
- 24. 如何發送電子郵件到特定的電子郵件?
- 25. 如何從電子表格發送電子郵件
- 26. 你如何從Android的任何電子郵件地址發送電子郵件?
- 27. 從VBA電子郵件程序發送HTML電子郵件
- 28. 如何使用java郵件從android發送電子郵件
- 29. 如何發送電子郵件在Java
- 30. 如何發送UTF-8電子郵件?
我想你是問你是否需要一個電子郵件帳戶從ASP.Net發送電子郵件?請澄清你的問題。 – Rumplin