如何在C#中通过Window Services发送电子邮件 [英] How to send Email through Window Services in C#

查看:63
本文介绍了如何在C#中通过Window Services发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谁能告诉我我的代码有什么问题.当我尝试向他们发送电子邮件时,出现以下错误:

异常详细信息:System.Net.Mail.SmtpException:操作已超时超时.


这是我的代码.

 公共 布尔 SendEmail()
        {
            列表< dbomjobqueue> endingJobQuoueList = getPendingJobQuoue();
            
                 foreach ( var 列表 in 中的未决作业,JobQuoueList)
                {
                    如果(list.fromStatus ==  0 )
                    {
                        ID =清单.ID;
                        toEmail = list.toEmail;
                        fromPath = list.fromPath;
                        fromStatus = list.fromStatus;
                        subject = " ;
                        // 创建邮件对象的实例
                        MailMessage mailMsg =  MailMessage();
                        MailAddress mailAddress = 尝试
                        {
                            // 设置内容
                            mailMsg.IsBodyHtml =  true ; // 将邮件正文的格式设置为HTML 
                            mailMsg.To.Add(toEmail); // 设置邮件的收件人地址
                            mailAddress =  MailAddress(" );
                            mailMsg.From = mailAddress; // 设置邮件的发件人地址
                            mailMsg.Subject =主题; // 设置邮件的主题
                            mailMsg.Body = " ; // 设置邮件的正文
                            mailMsg.Priority = MailPriority.High; // 将邮件的优先级设置为高
                            mailMsg.Attachments.Add( Attachment(fromPath)); // 创建并添加附件
                            SmtpClient smtpClient =  SmtpClient(); //  smtp服务器地址
                            System.Net.NetworkCredential凭据=  System.Net.NetworkCredential();
                            smtpClient.Credentials =凭据;
                            //  smtpClient.Host ="localhost"或"192.168.30.137"; 
                            smtpClient.EnableSsl =  true ;
                            //  smtpClient.UseDefaultCredentials = false; 
                            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                            如果(ID!=  0 )
                            {
                                fromStatus =  1 ; // 发送进行中
                                UpdateDbStatus();
                            }
                            smtpClient.Send(mailMsg);
                            eventLogDBOMJobQueue.WriteEntry(" );
                            //  fromStatus = 2;//通过电子邮件成功发送(完成)
                            //  UpdateDbStatus(); 
                        }
                        捕获(例外)
                        {
                            errorDescription = ex.Message;
                            //  fromStatus = 3;//错误
                            //  UpdateDbStatus(); 
                            返回 ;
                        }
                        最终
                        {
                            mailMsg = ;
                            mailAddress = ;
                        }
                 }
            }
            返回 ;
} 


这是我的配置.

 <   system.net  > 
    <   mailSettings  > 
      <   smtp  > 
        <  网络    ="   smtpcorp.com" 端口   465 " 用户名  用户名" 密码  密码"  defaultCredentials   ="    / > 
        <!-  <网络主机="smtp.gmail.com "port =" 465"/>  <  /smtp  > 
    <  /mailSettings  > 
  <  /system.net  >  


请帮帮我.

提前致谢.

解决方案



我首先看到的是您已经在配置的mailSettings元素中指定了凭据,但是您已将使用默认凭据设置为true.您需要将其设置为false,否则它将使用服务运行所在的本地安全性原则.

您的主机也可能无效,您可以使用telnet对此进行测试.这里有一篇很棒的文章,解释了如何执行此操作:

如何在Windows Server 2003中手动测试SMTP服务 [ host ="smtpcorp.com" port ="465"


确保端口是开放的.此外,请检查是否没有防火墙问题.您可以与IT管理员联系,并确保使用的配置正确.

另外,请查看此博客文章:
端口= 587,
EnableSsl = true,



非常感谢.


Hi,

Can anybody tell me what wrong in my code.When I tried to send them emails, I got the following error:

Exception Details: System.Net.Mail.SmtpException: The operation has timed out.


Here is my code.

public  bool SendEmail()
        {            
            List<dbomjobqueue> pendingJobQuoueList = getPendingJobQuoue();
            
                foreach (var list in pendingJobQuoueList)
                {
                    if (list.fromStatus == 0)
                    {
                        ID = list.ID;
                        toEmail = list.toEmail;
                        fromPath = list.fromPath;
                        fromStatus = list.fromStatus;
                        subject = "";
                        //Creating an instance of the mail object
                        MailMessage mailMsg = new MailMessage();
                        MailAddress mailAddress = null;
                        try
                        {
                            //Set the content
                            mailMsg.IsBodyHtml = true;  // Set the format of the mail message body as HTML
                            mailMsg.To.Add(toEmail);    // Set the recepient address of the mail message                     
                            mailAddress = new MailAddress("muna.angul10@gmail.com");
                            mailMsg.From = mailAddress; // Set the sender address of the mail message
                            mailMsg.Subject = subject;  // Set the subject of the mail message
                            mailMsg.Body = "";  // Set the body of the mail message
                            mailMsg.Priority = MailPriority.High;   // Set the priority of the mail message to high
                            mailMsg.Attachments.Add(new Attachment(fromPath));  //create and add the attachment
                            SmtpClient smtpClient = new SmtpClient();   //smtp server address
                            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                            smtpClient.Credentials = credentials;
                            //smtpClient.Host = "localhost"or"192.168.30.137";
                            smtpClient.EnableSsl = true;
                            //smtpClient.UseDefaultCredentials = false;
                            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                            if (ID != 0)
                            {
                                fromStatus = 1; //Sending In Progress 
                                UpdateDbStatus();
                            }
                            smtpClient.Send(mailMsg);                            
                            eventLogDBOMJobQueue.WriteEntry("Mail Send Successfully");
                            //fromStatus = 2;//Email send successfully(Done)
                            //UpdateDbStatus();
                        }
                        catch (Exception ex)
                        {                           
                            errorDescription = ex.Message;
                            //fromStatus = 3;//Error
                            //UpdateDbStatus();
                            return false;
                        }
                        finally
                        {
                            mailMsg = null;
                            mailAddress = null;
                        }
                 }
            }
            return true; 
}


And here is my configration.

<system.net>
    <mailSettings>
      <smtp>
        <network host="smtpcorp.com" port="465" userName="username" password="password" defaultCredentials="true" />
        <!--<network host="smtp.gmail.com" port="465" />-->
      </smtp>
    </mailSettings>
  </system.net>


Please help me.

Thanks in Advance.

Hi,

First thing I can see is you''ve specified credentials in your mailSettings element of your config but you''ve set use default credentials to true. You need to set this to false otherwise it will use the local security principle in which the service is running.

Your host may also be invalid you can test this using telnet. There''s an excellent article which explains how to do this here:

How To Test SMTP Services Manually in Windows Server 2003[^]


Are you sure that the following configuration is correct?

host="smtpcorp.com" port="465"


Make sure that the port is open. Further, do check there is no firewall issues. You can talk to your IT admin guys about it and make sure that the configuration used is ok.

Also, have a look at this blog post: MSDN Blog: System.Net.Mail with SSL to authenticate against port 465[^]


there is some problem in host and port.
it worked when i try with

Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,



thank you very much frnds.


这篇关于如何在C#中通过Window Services发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆