C#dot net windows应用程序无法向指定的电子邮件ID发送电子邮件 [英] C# dot net windows application is not able to send email to specified e-mail id

查看:89
本文介绍了C#dot net windows应用程序无法向指定的电子邮件ID发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是Kanchan Sharma。



几天前我开发了一个警报系统。在这个系统中,我发送电子邮件给指定的电子邮件ID作为截止日期提醒。在启动所有工作正常,但知道有一个问题,即应用程序无法向电子邮件ID发送电子邮件警报。我需要告诉的一件事是,当我在visual studio中执行应用程序时,没有异常并且电子邮件被正确发送但是当我在系统上安装并执行相同时,则面临所述问题。代码如下: -



Hi,
This is Kanchan Sharma.

I have developed a alert system few days ago. in this system i am sending e-mail message to specified e-mail ids as due date alert. in starting all was working well but know there is a problem that application is not able to send e-mail alert to e-mail ids. one thing i need to tell is that when i am executing the application in visual studio then there is no exception and e-mail is sent properly but when i install this on system and execute the same then the said problem is faced. code is given below:-

private void send_email_alert()
        {
            if (listView0.Items.Count > 0)
            {
                string msg = "Dear Sir/Madam<br>Please note that renewable/regular premium of given Insurance Policies are due:-<br><br>";
                msg = msg + "";
                //msg = msg + "<table border="1" cellpadding="1" cellspacing="1"><tbody><tr><td>SR.NO.</td><td>INSURED NAME</td><td>POLICY NUMBER</td><td>COMPANY NAME</td><td>DUE DATE</td></tr></tbody></table>";

                int ckk = 0;
                for (int kk = 0; kk < listView0.Items.Count; kk++)
                {
                    if (listView0.Items[kk].SubItems[4].Text == "0")
                    {
                        ckk++;
                        msg = msg + "";
                        //msg = msg + "";
                    }
                }
                msg = msg + "<table><tbody><tr><td>SR.NO.</td><td>INSURED NAME</td><td>POLICY NUMBER</td><td>COMPANY NAME</td><td>DUE DATE</td></tr><tr><td>" + ckk + "</td><td>" + listView0.Items[kk].SubItems[0].Text + "</td><td>" + listView0.Items[kk].SubItems[1].Text + "</td><td>" + listView0.Items[kk].SubItems[2].Text + "</td><td>" + listView0.Items[kk].SubItems[3].Text + "</td></tr><tr><td>" + ckk + "</td><td>" + listView0.Items[kk].SubItems[0].Text + "</td><td>" + listView0.Items[kk].SubItems[1].Text + "</td><td>" + listView0.Items[kk].SubItems[2].Text + "</td><td>" + listView0.Items[kk].SubItems[3].Text + "</td></tr></tbody></table>";

                if (ckk > 0)
                {
                    int chkalrt = 0;
                    string mail_srvr = "";
                    int mail_port = 0;
                    string authID = "";
                    string authPass = "";
                    string mail_to = "";
                    try
                    {
                        System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
                        con.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data source=.\\MSCFRM.mdb";
                        con.Open();
                        System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT alrt_sts_mail, mail_srvr, mail_to, smtpPort, authEmailId, authPass from alert_setting_tab where sec_name = 'INSURANCE'", con);
                        System.Data.OleDb.OleDbDataReader dr;
                        dr = cmd.ExecuteReader();
                        dr.Read();
                        chkalrt = int.Parse(dr["alrt_sts_mail"].ToString());
                        mail_srvr = dr["mail_srvr"].ToString();
                        mail_to = dr["mail_to"].ToString();
                        mail_port = int.Parse(dr["smtpPort"].ToString());
                        authID = dr["authEmailId"].ToString();
                        authPass = dr["authPass"].ToString();
                        dr.Close();
                        con.Close();
                    }
                    catch (Exception exp)
                    {
                        chkalrt = 0;
                        mail_srvr = "";
                        mail_to = "";
                        MessageBox.Show("Could not get the Insurance Premium e-mail alert detail, due to internal error. It will be retried again on next alert. \nError: " + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    if (mail_to == "" || mail_srvr == "")
                    {
                        MessageBox.Show("Could not sent the Insurance Premium e-mail alert. Receiver E-mail ID or E-Mail Hosting Server detail are not available.", "Alert : Null values found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    if (chkalrt == 1)
                    {
                        try
                        {
                            SmtpClient sc = new SmtpClient();
                            MailMessage mail_msg = new MailMessage();
                            MailAddress madd;

                            //assigning smtp server host through which mail is to be send
                            sc.Host = mail_srvr;
                            sc.Port = mail_port;
                            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(authID, authPass);
                            sc.Credentials = nc;

                            //if (ValidateEmail(FromTextBox.Text) == true)
                            //{
                            //    sendfrom = FromTextBox.Text;
                            //}
                            //else
                            //{
                            //    MessageBox.Show("Mail Address is Invalid or Subject is Blank.", "Alert : Invalid values", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            //    return;
                            //}

                            madd = new MailAddress("pilrs_system_alert@comethandicrafts.com");
                            mail_msg.From = madd;

                            //if (ValidateEmail(ToTextBox.Text) == true)
                            //{
                            mail_msg.To.Add(mail_to);
                            //}
                            //else
                            //{
                            //    MessageBox.Show("Mail Address is Invalid or Subject is Blank.", "Alert : Invalid values", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            //    return;
                            //}

                            //if (CcTextBox.Text != "")
                            //{
                            //    mail_msg.CC.Add(CcTextBox.Text);
                            //}
                            //if (BccTtextBox.Text != "")
                            //{
                            //    mail_msg.Bcc.Add(BccTtextBox.Text);
                            //}

                            mail_msg.Subject = "Insurance Policy Renewable/Regular Premium Due Alert";

                            msg = msg + "<br><br>Regards<br>PILRS SYSTEM (Phone-Insurance-LoansEmi-Registration-SaleChart Management System)<br><br>This is an auto generated mail, please do not reply.";


                            mail_msg.IsBodyHtml = true;
                            mail_msg.Body = msg;                           

                            //if (AttachmentTextBox.Text != "")
                            //{
                            //    Attachment atch;
                            //    string sfile = AttachmentTextBox.Text;
                            //    atch = new Attachment(sfile);
                            //    mail_msg.Attachments.Add(atch);
                            //}

                            //sc.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted);
                            //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


                            //send mail to reciever

                            sc.Send(mail_msg);

                            update_due_sts();

                            //mail_msg.Dispose();
                            //sc.Dispose();

                            //if (mail_sent == true)
                            //MessageBox.Show("Insurance message is sent succesfully.", "Alert : Sent", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show("Could not sent the Insurance Premium e-mail alert, due to internal error. It will be retried again on next alert. \nError: " + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }            
        }



[edit]已添加代码块 - OriginalGriff [/编辑]



我尝试了什么:



c#的自我评估dot net windows应用程序无法将电子邮件发送到指定的电子邮件ID


[edit]Code block added - OriginalGriff[/edit]

What I have tried:

Self assesment for c# dot net windows application is not able to send email to specified e-mail id

推荐答案

首先编辑您的问题,并删除已注释掉的代码 - 它不是'与问题有关,所以我们不需要它;或者想要它,它只会阻碍它。



然后尝试缩小你的问题。我们无法在相同的情况下运行您的代码,因此我们可以获得除了您给我们的信息之外的任何信息 - 坦白说,这不是很多。因为它可以在您的开发系统中运行,但不能在生产环境中运行,但我们无法模仿您所做的事情。



因此您必须记录正在发生的事情 - 好与坏 - 所以你可以查看它。添加语句以将过程添加到日志文件中,确切地说会发生什么:日期和时间戳记,记录您要发送的内容以及无法发送的内容。它可能很简单,因为您的电子邮件系统有一个限制,例如,阻止您每小时发送超过1000封电子邮件(我的托管服务会阻止它被用作垃圾邮件服务器场)。或者可能是电子邮件地址或电子邮件内容存在共同因素,导致其无法正常工作。或者可能是授权细节仅适用于您的开发机器。

我们不知道 - 我们无法找到,因为我们无法访问您的系统或电子邮件服务器 - 所以你必须收集信息,然后用细齿梳子开始查看。
Start by editing your question, and ripping out the commented out code - it isn't relevant to the problem, so we don't need it; or want it, it just gets in the way.

Then try to narrow your problem down. We can't run your code under the same circumstances you can, so we can;t get any information other than what you give us - which is not a lot, frankly. And because it works in your development system but not in production, we can't emulate exactly what you do anyway.

So you will have to log what is happening - good and bad - so you can review it. Add statements to add to a log file the process through that, and exactly what happens: date and time stamp it, record what you are sending, and what fails to send. It may be as simple as your email system has a throttle which prevents you sending more than 1000 emails an hour for example (my hosting service does to prevent it being used as a spam email farm). Or it may be that there is a common factor with the email addresses, or the email content which prevents it working. Or it may be that the authorisation details only work on your dev machine.
We don't know - and we can't find out because we don't have access to your systems or email server - so you are going to have to gather info, and start looking at it with a fine tooth comb.


这篇关于C#dot net windows应用程序无法向指定的电子邮件ID发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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