Sending email using ASP.NET - asp.net

I am trying to send email using ASP.NET through Gmail server. This is my code, can you help me how to resolve the timeout issue.
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
mail.IsBodyHtml = true;
mail.Priority = System.Net.Mail.MailPriority.High;
mail.From = new MailAddress("FromEmail#gmail.com");
mail.To.Add("ToEmail#gmail.com");
mail.Subject = "Fees Reminder";
mail.Body = "Please, Student ID: " + username + " pay your fees " + sqlFeesValue + ".";
mail.Body += " <html>";
mail.Body += "<body>";
mail.Body += "<table>";
mail.Body += "<tr>";
mail.Body += "<td>User Name : </td><td>" + username + "</td>";
mail.Body += "</tr>";
mail.Body += "<tr>";
mail.Body += "<td>Fees : </td><td> " + sqlFeesValue.ToString() +"</td>";
mail.Body += "</tr>";
mail.Body += "</table>";
mail.Body += "</body>";
mail.Body += "</html>";
smtp.Port = 465;
smtp.Credentials = new System.Net.NetworkCredential("email#gmail.com", "passwd");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
}
catch (Exception error)
{
lblMessage.Visible = true;
lblMessage.Text = error.Message;
}
Thanks in advance

Try using a different port number.
Gmail will accept port 25 or 587 when sending mail but times out using port 465.
Also make sure you have UseDefaultCredentials = False also.

Related

How to create log file on sending email using ASP.NET

I want to create a log file after sending an email on button click by using the below code
MailMessage mm = new MailMessage("emailid", "emialid");
mm.Subject = "GridView Email";
mm.Body = "GridView:<hr />" + sw.ToString();
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridViewPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.office365.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "emailid";
NetworkCred.Password = "password;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
string script = "<script type=\"text/javascript\">alert('Email Send Successfully');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
here
is a article write some text to text file. You could use for logging.
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
...
// Open the file to read from.
string readText = File.ReadAllText(path);

I want to code to send mail and message in asp.net

This is my code:
string from = "rashmi#example.com";
string to = "rashmi#example.com";
string mailSubject = "mail demo";
string mailBody = "mail sent successfully";
MailMessage mess = new MailMessage(from, to, mailSubject, mailBody);
mess.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient("192.168.43.1", 25); //Server ip & port
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = new System.Net.NetworkCredential("rashmi#example.com", "****password****");
try
{
emailClient.Send(mess);
Response.Write("message sent");
}
catch (Exception ex)
{
Response.Write("Exception caught in CreateTestMessage1(): {0}"+ ex.ToString());
}
but I get this error:
Exception caught in CreateTestMessage1(): {0}System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 192.168.43.1 (192.168.43.1:25), connect error 10061
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message) at mail_demo_project.register_mail_demo.submit_Click(Object sender, EventArgs e) in e:\dot net\projects\mail demo project\mail demo project\register_mail_demo.aspx.cs:line 53
error occured...
Use this code to send email:
string From = "fromemailaddress#gmail.com";
MailAddress mailAddress = new MailAddress(From, "Contact Us");
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("toemailaddress#gmail.com");
mailMessage.From = mailAddress;
mailMessage.Subject = Txt_Subject.Text;
string mailBody = "User Email: " + Txt_msg.Text + "<br/> User Name: " + Txt_Name.Text + "<br/> Message: " + Txt_msg.Text ;
mailMessage.Body = mailBody;
mailMessage.Priority = MailPriority.Normal;
mailMessage.IsBodyHtml = true;
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(From, "password");
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = credential;
smtpClient.Send(mailMessage);
and you have to make some security modification in your gmail account in security section :Allow less secure apps: YES

Sending mail in asp.net using Exchange Server(smtp-outlook.office365.com)

We are working in Mailing system in asp.net,our client using Microsoft exchange server outlook.office365. We are getting this exception.
"System.Net.Mail.SmtpException: The operation has timed out " error .Please help us.
btn_click(){
try{SmtpClient smtp = new SmtpClient("smtp.outlook.office365.com");
smtp.Host = "outlook.office365.com";
smtp.Port = System.Convert.ToInt32("443");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("userid", "mypwd");
smtp.Credentials = cred;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.TargetName = "STARTTLS/outlook.office365.com";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("frommailid", "username");
msg.To.Add(new MailAddress("xxx#xxx.com"));
msg.Subject = "Test";
msg.Body = "Test mail ";
smtp.Timeout = 60000;
smtp.Send(msg);
}catch(exception ex)
{
throw ex;
}
}
Refer the below code snippet and try this out. It worked for me.
Remember: From field should be same email id which is used as an SMTP user. So in this example, xxx#blabla.onmicrosoft.com should be the From email id.
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(ToField.Text));
mail.From = new MailAddress(FromField.Text); //From field should be same email id which is used as a SMTP user.
mail.Subject = "Email Subject";
mail.Body = "Message Body";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxx#blabla.onmicrosoft.com", "Password");
client.Port = 587;//You can use Port 25 if 587 is blocked
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.TargetName = "STARTTLS/smtp.office365.com";
try {
client.Send(mail);
}
catch (Exception ex) {
ResponseLabel.Text = "Error:" + ex.Message;
}

How to resolve the "A connection attempt failed" while sending mails from remote server

I am getting below error while sending mails from my webpage.
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.220.37.168:587
Please let me know how to resolve this error.My mail sending code is.
public void sendmail(string emailaddress, string batchid)
{
try
{
string msg1 = string.Empty;
string mailLink = #"http://tk5sdatweb02:2222/SitePages/Sales%20Desk%20Request%20A%20Service.aspx?MBUP=Yes&BatchID= "+batchid;
string strSubject = "Sales Desk Internal Bulk Upload";
string msg2 = string.Empty;
msg1 = "Test Email for" +batchid;
msg1 = "<html><head><body>";
msg2 = "<span style='font-size:14px;font-family:Calibri;'>Hi REDMOND\\v-rejinn";
msg2 = msg2 + #"<br/><br/><span style='font-size:14px;font-family:Calibri;'>A target list has recently been uploaded they will be send to SDAT.<br/>";
msg2 = msg2 + #"To view and/or validate the accounts, please visit the <a href='" + mailLink + "' style='color:#0000FF;text-decoration:none;'>MBUP Page</a><br />";
msg2 = msg2 + "If you have any questions or issues, please reach out to <a href='mailto:SDPET#microsoft.com'>SDPET#microsoft.com</a>.<br />";
msg2 = msg2 + "<br/> Thank you,<br/><b><font style='font-family:Segoe UI;font-size:28px;color:#033570;'> SALES</font>";
msg2 = msg2 + "<font style='font-family:Segoe UI;font-size:28px;color:#7ac366;'> DESK</font></b><br />";
msg2 = msg2 + "<font style='font-family:Segoe UI;font-size:10px;color:#3f6692;'> <span style='padding-left:11px;'>Enabling World Class Selling</span></font>";
msg2 = msg2 + "</span></body></head></html>";
//SmtpClient smtpClient = new SmtpClient();
SmtpClient smtpClient = new SmtpClient("tk5sdatsql01", 587);
//smtpClient.Host = "smtp.gmail.com";
MailMessage message = new MailMessage();
//MailAddress fromAddress = new MailAddress("sarath.hits#gmail.com", strSubject);
MailAddress fromAddress = new MailAddress("sdmail#microsoft.com");
message.From = fromAddress;
string strmailid = string.Empty;
strmailid = emailaddress;
message.To.Add(strmailid);
//message.Bcc.Add("sarath.psnr#gmail.com");
message.Subject = strSubject;
message.Body = msg2;
message.IsBodyHtml = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Port = 587;
//smtpClient.Credentials = new System.Net.NetworkCredential("sarath.hits#gmail.com", "myaccountdata");
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
smtpClient.EnableSsl = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
Response.Write("success");
}
catch (WebException ex)
{
}
}
There's nothing you can do other than catch the error - as the receiving mail server is not under your control.
However, if you think that the server should be responding perhaps you could try connecting to it using other means (like telnet) so you can verify the port or other requirements.

SMTP mail sending through ASP.Net

I have a problem regarding smtp mail sending with asp.net.When I attempt to send through the smtp protocol,I get the following error.
The server response was: 5.5.1 Authentication Required
I'm using something like this;
string mess = "";
mess += "<b>You have mail</b></br>";
mess += "<b>Name Surname</b>" + textName.Text + "</br>";
mess += "<b>Email adress</b>" + textEmail.Text + "</br>";
mess += "<b>Subject</b>" + textSubject.Text + "</br>";
mess += "<b>Message</b>" + textMessage.Text + "</br>";
mess += "<b>Date</b>" + DateTime.Now.ToString();
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add("oruc_esma#hotmail.com");
msg.From = new MailAddress("turkishcorpus1#gmail.com", "Esma oruc",System.Text.Encoding.UTF8);
msg.Subject = textSubject.Text;
msg.Body = mess;
SmtpClient smp = new SmtpClient();
smp.Credentials = new NetworkCredential("turkishcorpus1#gmail.com", "my password");
smp.Port = 587;
smp.Host = "smtp.gmail.com";
smp.EnableSsl = true;
smp.Send(msg);
}
Thanks in advance,
For sending gmail through SMTP in ASP.NET, use this
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("turkishcorpus1#gmail.com", "my password")
};
using (var message = new MailMessage("turkishcorpus1#gmail.com", "oruc_esma#hotmail.com")
{
Subject = "This is the title of the mail",
Body = "This is the body"
//,IsBodyHtml = true //optional
})
{
smtp.Send(message);
}

Resources