Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello! Dev.
i Am working on project and the project is is ASP.net c#,
I want to send Email on signup to user and owner of that site that the demo user create his/her account, and the same thing i use on contact us page, but My email code is working fine on local server, but when i upload my site to live server, no email sending, that code is not working, i didn't face any error on live server and email also not sanded, but that code is working 100% fine on local server.

What I have tried:

What i am tried is below.

This is Email.cs Class

public static bool SendEmail(string toadd, string subject, string msg, string cc = "", string bcc = "", string attachedfile = null)
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
try
{
string SendingEmail = "", MailServer = "", SendingPassword = "";
SendingEmail = "myemail.@gmail.com";
MailServer = "smtp.gmail.com";
SendingPassword = "myEmailPassword";
int ServerPort = 587;
message.IsBodyHtml = true;

message.From = new MailAddress(SendingEmail, "Driddle");
message.To.Add(new MailAddress(toadd.ToString()));

if (!string.IsNullOrEmpty(cc))
{
message.CC.Add(new MailAddress(cc.ToString()));
}

if (!string.IsNullOrEmpty(bcc))
{
message.Bcc.Add(new MailAddress(bcc.ToString()));
}
if (attachedfile != null)
{
Attachment attach = null;
string[] attachArr = attachedfile.Split('*');
foreach (string obj in attachArr)
{
attach = new System.Net.Mail.Attachment(obj);
message.Attachments.Add(attach);
}
}
message.Body = msg;
message.Subject = subject;
client.Host = MailServer;
client.Port = ServerPort;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(SendingEmail, SendingPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
message = null;
client = null;
}
}


This is my contact us page code,This code is working fine on local server Email sended successfully but when i upload this to server, this code didn't send any email and also there is no error on live server data save sucessfully to database but email is not working.


protected void Unnamed_Click(object sender, EventArgs e)
{
using(amazoneEntities db = new amazoneEntities()){


conatactU u = new conatactU();
u.Name = firstName.Text;
u.email = email.Text;
u.subject = phone.Text;

u.message = message.Text;
string Dat = DateTime.Now.ToString("");
u.MessageDate = Convert.ToDateTime(Dat);
u.status1 = "1";

db.conatactUs.Add(u);
db.SaveChanges();
var query = db.contactEmail(email.Text).ToList();
if (query.Count() > 0)
{

string sellerEmail = email.Text;
string subject = phone.Text;

Email.SendEmail(sellerEmail, "Thanks For Contacting Us", "Your Message Received Successfully. You will be answer within 24 hours ,Due to Large amount of queries");
alert.SendEmail(subject, "SMS ALERT", "Some one want to contact us with this subject" + "<br>" + subject + "<br>" + "With this Email Address" + "<br>" + sellerEmail);

}


}

}
Posted
Comments
[no name] 22-Mar-21 13:17pm    
ISP's limit email sending to the client's IP address; not a random remote address; unless you connect directly (roaming). Use a "service" when not using "local." For low volumes, there are free ones.
https://www.smtp2go.com/
Richard Deeming 23-Mar-21 5:12am    
client.UseDefaultCredentials should probably be false.

You're throwing away any exception details, so you have no idea what the problem is. It could be any number of issues - the firewall on the server blocking outbound traffic on port 587, Google rejecting connections from your server's IP address, etc.

Change your code to log the exception, and then investigate the error. Until you do that, you're just stumbling around in the dark trying to fix your code.
Member 14976405 23-Mar-21 5:44am    
Thanks for the answer, But i didn't know how to creat log file of email sending code! can you guide me in that? please

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900