【问题标题】:Error on Sending mail asp.net via console applications通过控制台应用程序发送邮件 asp.net 时出错
【发布时间】:2011-03-27 08:06:32
【问题描述】:

我已经编写了一个测试控制台应用程序,用于从消息队列中检索数据和发送邮件。我的控制台应用程序位于同一个 我的主要项目的解决方案。在我的消息队列中存在邮件消息。当我运行我的控制台应用程序时,会调用 邮件发送方法,但问题是邮件没有被发送。看到我给出的mailSettings 当我通过控制台应用程序发送邮件时,未应用 web.config 文件。因为当我直接发送邮件时没有 在控制台应用程序的帮助下,消息正在成功发送。此外,当我在 源代码本身通过 smtp 属性邮件正在通过控制台应用程序发送。但我想通过控制台应用程序发送邮件 web.congi 上设置的邮件设置。

我该如何克服这个问题。

最好的问候。

我在 web.config 中的设置。

我的邮件发送代码

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body,List attachments) { // 实例化一个新的 MailMessage 实例 MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);

        // Set the recepient address of the mail message
        //string[] arrEmailTo = to.Split(',');
        //foreach (string itemTo in arrEmailTo)
        //{

        //    //mMailMessage.To.Add(new MailAddress(itemTo));
        //}
        mMailMessage.To.Add(to);

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }

        // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }

        // Set the subject of the mail message
        mMailMessage.Subject = subject;

        //mMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
        //int Idd=BitConverter.ToInt32(Encrypt((Id.Id).ToString()),0);
        // byte[] IdHash = Encrypt((Id.Id).ToString());//generating hassh value
        //int IdHash1 = BitConverter.ToInt32(IdHash, 0);
        //passing the hash value by encoding it


        // Set the body of the mail message
        mMailMessage.Body = body;

        //include the attachments if any

        if(attachments!=null)

            foreach (string file in attachments)
            {
                Attachment aa = new Attachment(UpPath+file);
                mMailMessage.Attachments.Add(aa);
            }

        // Secify the format of the body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.High;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        mSmtpClient.EnableSsl = true;

//********* //在源代码中设置这些选项后,邮件可以正常发送 //******

        //mSmtpClient.Host = "smtp.gmail.com";
        //mSmtpClient.Port = 25;
        // Send the mail message
        //System.Net.NetworkCredential s = new System.Net.NetworkCredential("username@gmail.com", "password");
        //mSmtpClient.Credentials = s;
        //mSmtpClient.Port = 587;
        mSmtpClient.Send(mMailMessage);

//********* //在源代码中设置这些选项后,邮件可以正常发送 //****** }

提前致谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    只是为了您的测试,使用您的邮件设置将 App.Config 添加到控制台项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2013-11-23
      • 2020-08-24
      • 2016-02-23
      • 1970-01-01
      相关资源
      最近更新 更多