SmtpException 类

本文介绍如何使用C#的SmtpClient类发送电子邮件,并实现当遇到邮箱繁忙或不可用时的重试机制。通过具体的代码示例,展示了如何配置邮件地址、主题、正文等内容,并处理可能出现的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://msdn2.microsoft.com/zh-cn/library/system.net.mail.smtpexception(VS.80).aspx

public   static   void  RetryIfBusy( string  server)
{
    MailAddress from 
=   new  MailAddress( " ben@contoso.com " );
    MailAddress to 
=   new  MailAddress( " jane@contoso.com " );
    MailMessage message 
=   new  MailMessage(from, to);
    
//  message.Subject = "Using the SmtpClient class.";
    message.Subject  =   " Using the SmtpClient class. " ;
    message.Body 
=   @" Using this feature, you can send an e-mail message from an application very easily. " ;
    
//  Add a carbon copy recipient.
    MailAddress copy  =   new  MailAddress( " Notifications@contoso.com " );
    message.CC.Add(copy);
    SmtpClient client 
=   new  SmtpClient(server);
    
//  Include credentials if the server requires them.
    client.Credentials  =  (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine(
" Sending an e-mail message to {0} using the SMTP host {1}. " ,
         to.Address, client.Host);
    
try
    {
        client.Send(message);
    }
    
catch  (SmtpFailedRecipientsException ex)
    {
        
for  ( int  i  =   0 ; i  <  ex.InnerExceptions.Length; i ++ )
        {
            SmtpStatusCode status 
=  ex.InnerExceptions[i].StatusCode;
            
if  (status  ==  SmtpStatusCode.MailboxBusy  ||
                status 
==  SmtpStatusCode.MailboxUnavailable)
            {
                Console.WriteLine(
" Delivery failed - retrying in 5 seconds. " );
                System.Threading.Thread.Sleep(
5000 );
                client.Send(message);
            }
            
else
            {
                Console.WriteLine(
" Failed to deliver message to {0} " , ex.FailedRecipient[i]);
            }
        }
    }
}

转载于:https://www.cnblogs.com/Athrun/archive/2006/11/07/552556.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值