NET 2.0 发送EMail邮件

protected void Button1_Click( object sender, EventArgs e)
    
... {
         System.Net.Mail.MailMessage message
= new System.Net.Mail.MailMessage();
        
//收件人
         message.To.Add(txtMailAddress.Text);
        
//抄送
        foreach(string mailAddress in txtCopyAddress.Text.Split(','))
        
...{
            
if (!string.IsNullOrEmpty(mailAddress))
            
...{
                 message.CC.Add(mailAddress);
             }

         }
        
        
//主题
         message.Subject = txtSubject.Text;

         message.From
= new System.Net.Mail.MailAddress(fromAddress);
         message.Body
= txtMailContents.Text;
        
//发送附件
        if (fulAttachment.HasFile)
        
...{
             System.Net.Mail.Attachment attachment
= new System.Net.Mail.Attachment(fulAttachment.PostedFile.FileName);
             message.Attachments.Add(attachment);
         }

         System.Net.Mail.SmtpClient smtp
= new System.Net.Mail.SmtpClient(txtSMTPAddress.Text);
        smtp.Credentials
= new System.Net.NetworkCredential(txtUserName.Text,txtPassword.Text);
        
        
//smtp.usedefaultcredentials = true;
          
        
try
        
...{            
             smtp.Send(message);
             lblReturnMessage.Text
= "Mail Send Success!";
         }

        
catch (System.Net.Mail.SmtpException ex)
        
...{
             lblReturnMessage.Text
= "Send Error:" + ex.Message;
         }

      }

如果选择了163的SMTP服务器,“smtp.163.com”,发件人必须是登录用户的163 email用户地址。

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient( " smtp.163.com " );
smtp.Credentials
= new System.Net.NetworkCredential( " 163emailaddress " ,"emailpassword");

System.Net.Mail.MailMessage message
= new System.Net.Mail.MailMessage();
message.From
= new System.Net.Mail.MailAddress( " [email protected] " );


web.config加入

<system.net>
   <mailSettings>
    <smtp from="[email protected]">
     <network host="smtp.163.com" password="*****" port="25" userName="username" defaultCredentials="false"/>
    </smtp>
   </mailSettings>
</system.net>

以下为VB.NET代码

Dim message As New MailMessage()
            '收件人
            message.To.Add("[email protected]")
            '主题
            message.Subject = "使用.NET 2.0发送邮件"
            message.From = New MailAddress("[email protected]")
            message.Body = "非常简单"
            Dim m_smtpClient As New SmtpClient("smtp.163.com")
            m_smtpClient.Send(message)

你可能感兴趣的:(email)