If this problem occurs, you must use system. Web. Mail. Discard it. It is outdated. Use System. net. Mail.
Public class netsendmail
{
/**/
/// <Summary>
/// Brief description of the email sending class
/// </Summary>
# Region data member
// Recipient address
Private string m_to = "";
// Sender address
Private string m_from = "";
// Mail title
Private string m_subject = "";
// Body of the email
Private string m_body = "";
// Sending server name or address
Private string m_host = "";
// Sender's username
Private string m_username = "";
// Sender Password
Private string m_password = "";
// Email attachment
Private string m_file = "";
# Endregion
# Region Constructor
/**/
/// <Summary>
/// Overload the constructor
/// </Summary>
/// <Param name = "to"> recipient address </param>
/// <Param name = "from"> sender address </param>
/// <Param name = "subject"> email title </param>
/// <Param name = "body"> email body </param>
/// <Param name = "host"> name or address of the email sending server </param>
/// <Param name = "username"> sender username </param>
/// <Param name = "password"> sender password </param>
Public netsendmail (string to, string from, string subject, string body, string host, string username, string password, string filename)
{
M_to =;
M_from = from;
M_subject = subject;
M_body = body;
M_host = host;
M_username = username;
M_password = password;
M_file = filename;
}
# Endregion
# region data attributes
// recipient address
Public string to
{< br> Get
{< br> return m_to;
}< br> set
{< br> m_to = value;
}< BR >}
// sender address
Public String from
{< br> Get
{< br> return m_from;
}< br> set
{< br> m_from = value;
}< BR >}
// mail title
Public String subject
{< br> Get
{< br> return m_subject;
}< br> set
{< br> m_subject = value;
}< BR >}
// email body
Public String body
{< br> Get
{< br> return m_body;
}< br> set
{< br> m_body = value;
}< BR >}
// server address (name)
Public String host
{< br> Get
{< br> return m_host;
}< br> set
{< br> m_host = value;
}< BR >}
// sender's username
Public String username
{< br> Get
{< br> return m_username;
}< br> set
{< br> m_username = value;
}< BR >}
// sender's password
Public String password
{< br> Get
{< br> return m_password;
}< br> set
{< br> m_password = value;
}< BR >}
// Email attachment
Public String File
{
Get
{
Return m_file;
}
Set
{
M_file = value;
}
}
# Endregion
/**/
/// <Summary>
/// Send an email
/// </Summary>
/// <Returns> whether the message is sent successfully </returns>
Public bool send ()
{
Try
{
// Obtain all recipient addresses
Char [] CH = {','};
String [] address = m_to.split (CH );
Mailaddresscollection alladdress = new mailaddresscollection ();
For (INT I = 0; I <address. length; I ++)
{
// Recipient address
Mailaddress toaddress = new mailaddress (Address [I]);
Alladdress. Add (toaddress );
// Sender address
Mailaddress fromaddress = new mailaddress (m_from );
// define the mail message
mailmessage MSG = new mailmessage (fromaddress, toaddress);
// mail title
MSG. subject = m_subject;
// email body
MSG. body = m_body;
// Set whether to send in HTML Format
MSG. isbodyhtml = true;
// Obtain all email attachments
Char [] Cr = {';'};
String [] file = m_file.split (CR );
For (INT n = 0; n <file. length; n ++)
{
If (file [N]! = "")
{
// Attachment object
Attachment DATA = new attachment (file [N], mediatypenames. application. octet );
// Attachment
Contentdisposition disposition = data. contentdisposition;
Disposition. creationdate = system. Io. file. getcreationtime (file [N]);
Disposition. modificationdate = system. Io. file. getlastwritetime (file [N]);
Disposition. readdate = system. Io. file. getlastaccesstime (file [N]);
// Add email attachments
MSG. attachments. Add (data );
}
}
// Use Simple Mail Transfer Protocol to send mail
Smtpclient Sendmail = new smtpclient ();
// The name or address of the server that sent the email
Sendmail. Host = m_host;
// Verify the sender's identity
Sendmail. Credentials = new networkcredential (m_username, m_password );
// Method for handling emails to be sent
Sendmail. deliverymethod = smtpdeliverymethod. Network;
// Send an email
Sendmail. Send (MSG );
}
Return true;
}
Catch (exception ex)
{
Return false;
}
}
}