托管后,电子邮件发送代码无效 [英] Email sending code is not work after hosting

查看:65
本文介绍了托管后,电子邮件发送代码无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MailMessage mm = new MailMessage("xyz@gmail.com", TextBoxEmail.Text.Trim());
                                        mm.Subject = "Password Recovery";
                                        mm.Body = string.Format("Hi ,<br /><br />Your password is .<br /><br />Thank You.");
                                        mm.IsBodyHtml = true;
                                        SmtpClient smtp = new SmtpClient();
                                        smtp.Host = "smtp.gmail.com";
                                        smtp.EnableSsl = true;
                                        NetworkCredential NetworkCred = new NetworkCredential();
                                        NetworkCred.UserName = "xyz@gmail.com";
                                        NetworkCred.Password = "xyz";
                                        smtp.UseDefaultCredentials = true;
                                        smtp.Credentials = NetworkCred;
                                        smtp.Port = 587;
                                        smtp.Send(mm);
                                        message = "Registration successful. Activation email has been sent.";
                                        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);







这是我在注册成功后发送电子邮件的代码。它在我的localhost iis服务器上正常工作。但在服务器上部署网站后,电子邮件不会发送给用户。有




this is my code to send email after registration is successful. it work fine on my localhost iis server. but after deploying web-site on server email is not send to user. there is

Object reference not set to an instance of an object.



显示错误消息。请帮助。




error message shown. please help.

same code work on password recovery page. but here it is not working.





我在代码中做了一些更改并且还创建一个新页面进行注册,现在它显示以下错误



对象引用未设置为对象的实例。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.NullReferenceException:对象引用未设置为实例一个对象。



首先我认为这是邮件发送代码的错误,但它在iis本地服务器和其他页面上都能正常工作。所以我认为这可能是按钮点击事件的问题。能够到达这里。请帮助我,因为我知道我在这里得到答案。



我的新代码:







I did some changes in code and also create a new page for registration now it is showing a following error

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

first i thought that it is error of mail sending code but it work fine on iis local server and also on other page. so i think may be it is a problem of button click event. just able to reach here. please some help me as i know i get my answer here.

my new code:


using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("xyz@gmail.com");
                    mail.Subject = "mailSubject";
                    mail.Body = "mailBody";
                    mail.IsBodyHtml = true;

                    mail.To.Add("xyz@gmail.com");



                    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))//2nd parameter is PORT No.
                    {
                        smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "xyz");
                        smtp.EnableSsl = true;//set this as your Host Name properties, for gmail,its true
                        smtp.Send(mail);//actual sending operation here
                    }
                }





我是什么尝试过:





What I have tried:

MailMessage mm = new MailMessage("xyz@gmail.com", TextBoxEmail.Text.Trim());
                                        mm.Subject = "Password Recovery";
                                        mm.Body = string.Format("Hi ,<br /><br />Your password is .<br /><br />Thank You.");
                                        mm.IsBodyHtml = true;
                                        SmtpClient smtp = new SmtpClient();
                                        smtp.Host = "smtp.gmail.com";
                                        smtp.EnableSsl = true;
                                        NetworkCredential NetworkCred = new NetworkCredential();
                                        NetworkCred.UserName = "xyz@gmail.com";
                                        NetworkCred.Password = "xyz";
                                        smtp.UseDefaultCredentials = true;
                                        smtp.Credentials = NetworkCred;
                                        smtp.Port = 587;
                                        smtp.Send(mm);
                                        message = "Registration successful. Activation email has been sent.";
                                        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);







这是我在注册成功后发送电子邮件的代码。它在我的localhost iis服务器上正常工作。但在服务器上部署网站后,电子邮件不会发送给用户。请帮助。




this is my code to send email after registration is successful. it work fine on my localhost iis server. but after deploying web-site on server email is not send to user. please help.

推荐答案

可能是你的NetworkCred值:我知道你已经在代码中替换了值,但我怀疑gmail拒绝你的凭据。尝试更改:

Probably, it's your NetworkCred values: I know you have substituted values into the code, but I suspect that gmail is rejecting your credentials. Try changing:
smtp.UseDefaultCredentials = true;

收件人:

To:

smtp.UseDefaultCredentials = false;

并仔细检查您的用户名和密码发送到Gmail。

And double check the username and password you are sending to gmail.


嗨会员_1979,



您可以尝试以下代码,如果它不起作用,请上传确切的错误信息,我们将正确地缩小问题范围。它也可能是由于某些网络相关问题不允许发送。



Hi Member__979,

You can try the below code and again if it doesnt work , then please upload the exact error message sothat we will be narrow down the issue properly. It also may due to some network related issue which is not allowing to send.

using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("sendersMailAddressHere");
                    mail.Subject = mailSubject;
                    mail.Body = mailBody;
                    mail.IsBodyHtml = true;

                    mail.To.Add("Toaddress");
                    mail.CC.Add("CCaddress");
                        

                    using (SmtpClient smtp = new SmtpClient("hostNameHere",587))//2nd parameter is PORT No.
                    {
			smtp.Credentials = new System.Net.NetworkCredential("sendersEmail", "sendersPassword");//supply userId & psd here
       			smtp.EnableSsl = true;//set this as your Host Name properties, for gmail,its true
                        smtp.Send(mail);//actual sending operation here
                    }
                }





谢谢,

Prateek



Thanks,
Prateek


这篇关于托管后,电子邮件发送代码无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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