I'm getting an error while sending email through asp.net SMTP server. It says operation has timed out. How to overcome the exception?
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("mymailid#hotmail.com");
mail.To.Add("yourmailid#gmail.com");
mail.Subject = "Verify your account";
mail.Body = "Your OTP for your account verification is "+OTP.ToString()+". Enter your OTP in verification window.";
mail.IsBodyHtml = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Timeout = 20000;
SmtpServer.Send(mail);
errmsgShow.Text = "Mail sent";
}
catch (Exception e)
{
//string errMsg = "alert('OTP sending failed. Error: " +e.ToString()+ "')";
//ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", errMsg, true);
//Console.WriteLine(e.Message.ToString());
errmsgShow.Text = e.ToString();
}
I have also included below in the web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.live.com" port="25" userName="mymailid#hotmail.com" password="password" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
Try removing SmtpServer.Timeout = 20000;.
Update:
i see you have SmtpClient SmtpServer = new SmtpClient();.
SmtpServer is a property of SmtpMail SmtpMail.SmtpServer. Try changing the variable name, something like
SmtpClient mailClient = new SmtpClient();
Related
I have a script sending mail from my .net application.
My hosting provider requires the emails to be authenticated if I don't want to cause delays in receiving them (currently delay is almost a day from sending).
I have build mail sending script with authentication but it doesn't work - it doesn't produce any error or exception but the emails are still not received instantly.
In the other hand similar script made in classic asp works and I receive mails instantly.
Am I missing something?
This is asp script (which works):
<%
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "sender#mydomain.com"
objEmail.To = "recipient#gmail.com"
objEmail.Subject = "Test Mail"
objEmail.Textbody = "Test Mail"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myusername"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
objEmail.Configuration.Fields.Update
objEmail.Send
If Not err.number=0 Then
Response.write "ERROR: " & err.Description
err.Clear
end if
%>
This is .NEt (which doesn't work):
public static void SendEmail(string senderName, string senderEmail, string recipient, string comments, string subject, bool inTemplate)
{
MailAddress from = new MailAddress(senderEmail);
MailAddress to = new MailAddress(recipient);
MailMessage mail = new MailMessage(from, to);
mail.ReplyToList.Add(senderEmail);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = comments;
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
NetworkCredential basicCredentials = new NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = mailSettings.Smtp.Network.Host;
smtpClient.Port = mailSettings.Smtp.Network.Port;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredentials;
try
{
smtpClient.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
These are my web.config settings:
<system.net>
<mailSettings>
<smtp>
<network host="mail.mydomain.com" userName="myusername" password="mypassword" port="25" />
</smtp>
</mailSettings>
</system.net>
The web.config settings are correct but even if I input server settings manually into the class - it doesn't work.
Thanks
Try this:
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add("test#hotmail.com");
// From
MailAddress mailAddress = new MailAddress("you#hotmail.com");
mailMsg.From = mailAddress;
// Subject and Body
mailMsg.Subject = "subject";
mailMsg.Body = "body";
// Init SmtpClient and send on port 587 in my case. (Usual=port25)
SmtpClient smtpClient = new SmtpClient("mailserver", 587);
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("username", "password");
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);
It turned out that the problem was at the hosting provider side. Not sure what they did but it eventually started to work.
I have created one mail.aspx.cs with following code
protected void Page_Load(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("sender#foo.bar.com");
message.To.Add(new MailAddress("recipient1#foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
}
And web.config settings
<system.net>
<mailSettings>
<smtp from="test#gmail.com">
<network host="smtpserver1" port="25" userName="abc xyz" password="abc" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
Getting failure error while sending mail on page load on following line
client.Send(message); mail sending fail,
i am new in asp.net,guide me correct way to solve this problem.
message.From = new MailAddress("sender#foo.bar.com");
And
<mailSettings>
<smtp from="nitinturankar3#gmail.com">
Must be the same!
You are using a username with empty space userName="NITIN TURANKAR", this is wrong
public string SendMail(string toList, string from, string ccList, string subject, string body)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
//if (attachments != "")
//{
// message.Attachments.Add(new Attachment(attachments));
//}
if (ccList != null && ccList != string.Empty)
message.Bcc.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
// We use gmail as our smtp client
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(
"Email", "password");
smtpClient.Send(message);
msg = "Successful<BR>";
string message1 = "Your Query has been Submited Successfully";
}
catch (Exception ex)
{
}
return msg;
}
try port replace to 587 Also.
I want to send an e-mail, but it shows the following error:
try
{
string filename = Server.MapPath("~/App_Data/FeedBack.txt");
string mailbody = System.IO.File.ReadAllText(filename);
mailbody = mailbody.Replace("##Name##", txtName.Text);
mailbody = mailbody.Replace("##Email##",txtEmail.Text);
mailbody = mailbody.Replace("##contact##",txtContact.Text);
mailbody = mailbody.Replace("##Commnect##",txtSuggestion.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Responce from website";
myMessage.Body = mailbody;
myMessage.From = new MailAddress("amitverma0511#gmail.com","Amit Kumar");
myMessage.To.Add(new MailAddress("amitverma1150#gmail.com", "Amit Verma"));
SmtpClient mysmptclient = new SmtpClient("localhost");
mysmptclient.Host = "smtp.gmail.com";
mysmptclient.Port = 587;
mysmptclient.Credentials = new System.Net.NetworkCredential("amitverma0511#gmail.com", "Myemailpassword");
mysmptclient.Send(myMessage);
formatTable.Visible = false;
lblMail.Text = "Your Feedback has been sent successfully";
lblMail.ForeColor = Color.Green;
}
catch(Exception expt)
{
lblMail.Text = "unable to sent the feedback .Check your smpt server"+expt.Message;
lblMail.ForeColor = Color.Red;
}
My web.config file looks like this:
<system.net>
<mailSettings>
<smtp from="amitverma0511#gmail.com">
<network host="smtp.gmail.com" port='587' password="myemailpass" userName="amitverma0511#gmail.com"/>
</smtp>
</mailSettings>
</system.net>
As far as I know, you are overwriting your web.config settings by using localhost (your IIS) as a SMTP Server.
If you have not configured your IIS to serve as a SMTP server this naturally will not work.
But, if you want to use localhost and not the google servers just use this 3 lines:
SmtpClient MySmtpClient = new SmtpClient();
MySmtpClient.UseDefaultCredentials = true;
MySmtpClient.Send(mailMessage);
You should remove the string localhost from SmtpClient mysmptclient = new SmtpClient("localhost"); and it should work, if all your other settings are correct.
Further information can be found here.
I want to send email using asp.net, I have configured my SMTP service and added 127.0.0.1 as relay. I used to my home the application was success but I installed on the office it cannot send email why?
here is the C# code
MailMessage objemail = new MailMessage();
objemail.To.Add(new MailAddress("apthodisiac#gmail.com"));
objemail.From = new MailAddress(Request.Form["inputEmail"].ToString());
objemail.Subject = Request.Form["inputSubject"].ToString();
objemail.Body = "Dari: " + Request.Form["inputName"].ToString() + "\n\n" +
"Phone: " + Request.Form["inputPhone"].ToString() + "\n\n" +
Request.Form["inputMsg"].ToString();
objemail.IsBodyHtml = true;
objemail.Priority = MailPriority.Normal;
SmtpClient objSmtpClient = new SmtpClient();
objSmtpClient.Send(objemail);
here is the web.config configuration
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="127.0.0.1" port="25" userName="yyyy" password="xxxxx" />
</smtp>
</mailSettings>
</system.net>
My office use proxy, is that the problem I cannot send email? please advice
May be port number is wrong try with port number "587" something as shown below:
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
you can try this:
objSmtpClient.EnableSsl = true
I want to send a confirmation email to user in registration page. The following code is the related part:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sc.Send(mail);
MESSAGE(true, "Sending mail is successful");
}
catch (Exception)
{
MESSAGE(true, "Sending mail is unsuccessful!");
}
But it does not send an email to related user. I have looked at forums and I added to web.config the following part:
<system.net>
<mailSettings>
<smtp from="myaddress#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="myaddress#gmail.com" password="password" />
</smtp>
</mailSettings>
</system.net>
But anything didn't change. Then i have debugged and it enters into the try statement and when it comes to sc.Send(mail);, it drops to catch. Where is my mistake?
Additionally, during debug i realized that it shows this error: cannot get IIS pickup directory. I have controlled whether I have a smtp service or not from services but i couldn' t see this service. Is this error related to that service?
Thanks in advance.
Why not use GMAIL as the SMTP server, since you are defining a GMAIL account, by defining the host as "smtp.gmail.com", and port at 587?
Specific details here: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
Thanks for your helps.
I have solved this problem. I have changed my code as the following one:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.EnableSsl = true;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "</body></html>";
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Send(mail);
MESAJ(true, "Sending mail is successful");
}
catch (Exception)
{
MESAJ(true, "Sending mail is unsuccessful!");
}
and then i have set my web.config like the following :
<mailSettings>
<smtp deliveryMethod="Network"
from="admin#gmail.com">
<network defaultCredentials="false" host="smtp.gmail.com" port="587"
userName="admin#gmail.com" password="password" />
</smtp>
</mailSettings>
that works.. :)