c# sending email operation timed out - c#

MailMessage msg = new MailMessage("teunenrichard#gmail.com", "ipadcraze#hotmail.com", "Movies this month", "Hello this is a test mail");
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
NetworkCredential xre = new System.Net.NetworkCredential("teunenrichard#gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = xre;
smtp.EnableSsl = true;
smtp.Send(msg);
This is the code i run in a form . load to do a test email but it will not rund and says operation timed out. ive tried everything please help
MessageBox.Show("mail sent");

Use the "Timeout" property for your Smtp client . I think 0 is max
smtp.Timeout = 0;

For better Understanding of the error message try putting your code in a Try-Catch Block and then see the inner exception of the catch in MessageBox.Show(). It might provide you some more information regarding the error and might help/guide you in the right direction to resolve it. Something like below:-
try
{
//your email sending logic
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}

You should send your mails using background thread, so as to make sure that your UI thread does not block and returns immediately. You can do something like this
private async void sendButton_Click(object sender, EventArgs e)
{
var result = await SendMail();
if (result)
{
MessageBox.Show("Mail sent");
}
}
private Task<bool> SendMail()
{
var task = Task.Run<bool>(() =>
{
MailMessage msg = new MailMessage("sendermail#gmail.com", "recievermail#gmail.com", "Movies this month", "Hello this is a test mail");
msg.IsBodyHtml = false;
using(SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.UseDefaultCredentials = false;
NetworkCredential xre = new NetworkCredential("sendermail#gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = xre;
smtp.EnableSsl = true;
smtp.Send(msg);
return true;
}
});
return task;
}

Related

Timeout on SMTP

I already tried everything and I am still receiving a timeout error. Can someone help me?
private void button1_Click(object sender, EventArgs e)
{
//Execute().Wait();
try
{
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.From = new MailAddress("senderEmail");
mail.To.Add(new MailAddress("myemail#gmail.com"));
mail.Subject = "test";
mail.Body = "test";
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.office365.com", 587/* or 25 */);
System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential("username", "password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
client.Timeout = 180000;
client.Send(mail);
MessageBox.Show("sucess");
}
catch (Exception ex)
{
MessageBox.Show("error");
MessageBox.Show(ex.ToString());
}
}
I'm running the application as the admin and I already tried other ports

How can I make my Xamarin app send an email whenever an error occurs?

My Xamarin app's release version is being tested by a non-developer, which sometimes means the tester has trouble communicating to me the details of bugs they encounter.
I'd like to implement some code that will execute whenever a crash or error occurs, and send an email to my address with the error information to help my debugging. I've written an email method (see below) but I'd like to know how I can call it whenever an unhandled exception is thrown rather than wrapping every single possible method in a try-catch.
public static void Email(string htmlString)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.From = new MailAddress("email here");
msg.To.Add(new MailAddress("other email here"));
msg.Subject = "Error Report";
msg.IsBodyHtml = true;
msg.Body = htmlString;
smtp.Port = 1234;
smtp.Host = "smtp here";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(msg);
}
catch (Exception)
{
}
}
The basic way to catch errors in Xamarin apps is by using a try/catch block. Try/catch is easy to get.
You could do something like below.
private void Send_Clicked(object sender, EventArgs e)
{
try
{
.......
}
catch(Exception ex)
{
Email(ex.ToString());
}
}
public static void Email(string htmlString)
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.From = new MailAddress("email here");
msg.To.Add(new MailAddress("other email here"));
msg.Subject = "Error Report";
msg.IsBodyHtml = true;
msg.Body = htmlString;
smtp.Port = 1234;
smtp.Host = "smtp here";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(msg);
}
Xamarin(Mono) internally "handles" those uncaught exceptions by surrounding literally everything with try-catch and raising the Unhandled event.
Explanation from the thread. Struggling to understand Xamarin exception handling
You could check the link. It provides a way to get the error log from log File.
Exceptions when using Xamarin Android

How to prevent code the being send duplicate email in asp.net?

I have a code on my website to send an email when a user clicks on a button. For some reasons, the code started sending an enormous number of duplicate emails. Actually, it stopped sending when it reached the GoDaddy daily limit, ie. 5000 emails!!!
Is there any way to prevent this situation? Do SMTP timeout helps in this situation?
try
{
SmtpClient client = new SmtpClient("sg2nlvphout-v01.shr.prod.sin2.secureserver.net", 25);
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("email#domain.com", "password");
MailMessage msgobj = new MailMessage();
msgobj.To.Add(email);
msgobj.From = new MailAddress("email#domain.com");
msgobj.Subject = "Subject";
msgobj.Body = body;
AlternateView altView = AlternateView.CreateAlternateViewFromString(msgobj.Body, null, MediaTypeNames.Text.Html);
msgobj.AlternateViews.Add(altView);
client.Send(msgobj);
}
catch (Exception ex)
{
}
try this,
try
{
bool flag = false;
if(!flag)
{
SmtpClient client = new SmtpClient("sg2nlvphout-v01.shr.prod.sin2.secureserver.net", 25);
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("email#domain.com", "password");
MailMessage msgobj = new MailMessage();
msgobj.To.Add(email);
msgobj.From = new MailAddress("email#domain.com");
msgobj.Subject = "Subject";
msgobj.Body = body;
AlternateView altView = AlternateView.CreateAlternateViewFromString(msgobj.Body, null, MediaTypeNames.Text.Html);
msgobj.AlternateViews.Add(altView);
client.Send(msgobj);
flag = true;
}
}
catch (Exception ex)
{
}

I have a error on progress bar when sending email

I would like to have a progress bar when sending the auto generated email but I'm getting errors. Please help me fix this. Any type of response is greatly appreciated.
I don't know if I'm on the right path, I'm a beginner on C# and just relying guides online so.
This line has the error
new System.Threading.Thread(new System.Threading.ThreadStart(btnSend_Click));
private void btnSend_Click(object sender, EventArgs e)
{
//Cursor.Current = Cursors.WaitCursor;
try
{
MailMessage loginInfo = new MailMessage();
string em = "angelicasarimo15#gmail.com";
loginInfo.To.Add(em.ToString());
loginInfo.From = new MailAddress("vicserna1997#gmail.com");
loginInfo.Subject = "Requesting Supplies";
loginInfo.Body = "We want another supplies for blah blah blah" + System.Environment.NewLine +
"This is a system generated email.";
loginInfo.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("vicserna1997#gmail.com", "*Pass*");
smtp.Send(loginInfo);
MessageBox.Show("Email has been sent!", "Sent", MessageBoxButtons.OK);
progressBar1.Visible = true;
progressBar1.Style = ProgressBarStyle.Marquee;
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(btnSend_Click));
thread.Start();
}
catch
{
MessageBox.Show("Message not sent please check you internet connection", "Not Sent", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
}
Use SendAsync and subscribe to SendCompleted. This will notify you the sending process is completed. I don't believe its possible to get the progress of the sending email.

error in sending mail from separate thread - asp.net/C#

I'm working in an application which sends the mail from server. since smtp.send(msg) will take some time to communicate with the server. i had made the send code block in separate thread. It worked fine before, but after adding the timer control timer1( which was doing some code logic). The mail send was interrupted due to the following error :
Unable to evaluate expression because the code is optimized or a
native frame is on top of the call stack
Threading comes here..
void sendMail()
{
ThreadStart sendCreateMail = delegate() { Send(subject); };
Thread threadSendCreateMail = new Thread(sendCreateMail);
sendCreateMail.IsBackground = true;
sendCreateMail.Start();
timer1.Enabled = true;
}
net.mail code comes here....
protected void Send(string subject)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.office365.com");
var credential = (System.Net.NetworkCredential)smtp.Credentials;
string Username = credential.UserName;
string password = credential.Password;
mail.From = new MailAddress(Username);
mail.To.Add(toMail);
mail.Subject = "subject";
mail.Body = "msg body";
mail.IsBodyHtml = true;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(Username, password);
smtp.Send(mail);
}
UPDATE
The sendMail task works in other pages. Here it is a popup so that timer1 block which i already told is doing a popup close function. There it is stopping the execution of thread. that i can understand (like response.end, response.redirect can't guess what exactly, its a third party tool called telerik radwindow!). but how to overcome this.
this is what i use in my application
SmtpClient client = new SmtpClient();
client.Port = your port;
client.Host = your host;
client.EnableSsl = true;
client.Timeout = 15000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(your username, your passowrd);
MailMessage mm = new MailMessage(your username, recepient[0], title, message);
for (int a = 1; a < recepient.Count; a++)
mm.To.Add(recepient[a]);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += (s, e) =>
{
client.Dispose();
mm.Dispose();
};
client.SendAsync(mm, null);

Categories

Resources