C# 用163邮箱发送邮件

main.cs类
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
ExpandedBlockStart.gifContractedBlock.gif
namespace Common{

    
public partial class MailEntity
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>        
        
/// /// default constructor       
        
///  /// </summary>       

        public MailEntity()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{ }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Send mail 
        
///  </summary>       
        
///  /// <param name="mailEntity">MailEntity object</param>
        
/// <returns></returns>       

        public bool SendMail(MailEntity mailEntity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Encoding encoding 
= mailEntity.Encoding;
                MailMessage mailMessage 
= new MailMessage(
                    
new MailAddress(mailEntity.MailFrom, mailEntity.DisplaySenderName, encoding), 
                    
new MailAddress(mailEntity.MailTo, mailEntity.DisplayReceiverName, encoding)
                    ); 
                mailMessage.SubjectEncoding 
= encoding; 
                mailMessage.Subject 
= mailEntity.Subject; 
                mailMessage.IsBodyHtml 
= mailEntity.IsBodyHtml; 
                mailMessage.Body 
= mailEntity.ContentBody; 
                SmtpClient smtpClient 
= new SmtpClient(mailEntity.Host);
                smtpClient.Credentials 
= new NetworkCredential(mailEntity.UserName, mailEntity.Pwd);
                smtpClient.Timeout 
= mailEntity.TimeOut; 
                smtpClient.Send(mailMessage); 
                
return true;
            }

            
catch(SmtpException sex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Ex 
= sex.Message;
                
return false;
            }

            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Ex 
= ex.Message;
                
return false;
            }

        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Properties#region Properties

        
public string Ex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
getset;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>        
        
/// the email send timeout       
        
/// </summary>       

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Int32 TimeOut setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>        
        
/// the email address that will send the email       
        
/// </summary>      

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String MailFrom setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>       
        
/// the email address that will receive the email        
        
/// </summary>    

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String MailTo setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>       
        
/// the Encoding type that will edcode the email content       
        
/// </summary>       

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Encoding Encoding setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>        
        
/// the name of the user who send the email        
        
/// </summary>  

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String UserName setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>       
        
/// the password of the user who send the email      
        
/// </summary>       

ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Pwd getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>        
        
/// the sender name that will display in the sender item        
        
/// </summary>    

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String DisplaySenderName setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>        
        
/// the receiver name that will display in the receiver item       
        
/// </summary>      

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String DisplayReceiverName setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>       
        
/// mail content       
        
/// </summary>        

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String ContentBody setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////  <summary>       
        
/// mail subject      
        
/// </summary>      

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Subject setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<summary>       
        
/// mail host address       
        
/// </summary>     

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Host setget; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<summary>        
        
/// if the mail body is in html format    
        
/// </summary>       


ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Boolean IsBodyHtml setget; }
        
#endregion

    }

}

发送方法:
ContractedBlock.gif ExpandedBlockStart.gif Code
        private void button1_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            Common.MailEntity mailEntity 
= new Common.MailEntity();
            mailEntity.MailFrom 
= "test@163.com";
            mailEntity.UserName 
= "test";
            mailEntity.Pwd 
= "123456";
            mailEntity.MailTo 
= "test@qq.com";
            mailEntity.DisplayReceiverName 
= "test";
            mailEntity.DisplaySenderName 
= "test";
            mailEntity.Subject 
= "Subject test";
            mailEntity.ContentBody 
= " ContentBody Text";
            mailEntity.Host 
= "smtp.163.com";
            mailEntity.TimeOut 
= 999999;
            mailEntity.Encoding 
= Encoding.GetEncoding("GB2312"); ;
            mailEntity.IsBodyHtml 
= true;

            
bool ma= mailEntity.SendMail(mailEntity);
            MessageBox.Show(ma.ToString());
        }

这个可以发送成功。

这里要注意的是,我用的邮箱是2004年注册的可以发送,但我用2009年注册的邮箱发送是不支持。
用QQ邮箱也不会支持,也就是说163对于新注册的用户(具体是以什么时间为分隔时间?)没有开放smtp功能,qq对所有用户也没有开放smtp功能。

转载于:https://www.cnblogs.com/chhuic/archive/2009/09/26/1574375.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值