Unable to send mail if one mail not present with ASP - asp.net

I've written code for sending a newsletter, all is working fine, but there is a problem if one of the email addresses in the list, doesn't exit or the domain does not exist.
In this case the script stops immediately and the sending of the mail list is not finished.
Here is the part of the code I want to modify.
public static void SendMessage(String sender, String recipient, String message, String object)
{
try
{
MailMessage mail = new MailMessage(sender, recipient);
mail.Subject = object;
mail.IsBodyHtml = true;
mail.Body = message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "my.smtp.com";
smtp.Send(mail);
}
catch (Exception e)
{ throw new Exception("AdminEmail - SendMessage >> recipient: " + recipient + " - generic error: " + e.Message); }
}
Hope somone can help me, thank you very much!

Welcome to SO.
From what I can infer from your description your are not handling the exception that is thrown by SendMessage.
Handle the exception in the caller method. Or do a dirty fix as shown below...
This is not a real fix. But will help you understand the issue...You have to determine in your calling method what to do if SendMessage throws an exception.
public static void SendMessage(String sender, String recipient, String message, String object)
{
try
{
MailMessage mail = new MailMessage(sender, recipient);
mail.Subject = object;
mail.IsBodyHtml = true;
mail.Body = message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "my.smtp.com";
smtp.Send(mail);
}
catch (Exception e)
{
//Just log error and continue to process
}
}

Related

Sending Mail while running the application in IIS

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(txtEmail.Text.ToString());
mailMessage.Subject = txtSubject.Text.ToString();
mailMessage.To.Add(new MailAddress("my#gmail.com"));
mailMessage.Body = txtSuggestion.Text.ToString();
mailMessage.IsBodyHtml = true;
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new NetworkCredential(txtEmail.Text.ToString(), txtPassword.Text.ToString());
sc.EnableSsl = true;
sc.Send(mailMessage);
lblMessage.Text = "Message Sent Successfully";
txtSubject.Text = "";
txtEmail.Text = "";
txtName.Text = "";
txtSuggestion.Text = "";
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
This is the code i am using to send email but whenever i am trying to run the application, i am getting error.
enter image description here
if someone can help why i am getting the error, that would be great.
When using SSL, try using port 465.
https://support.google.com/a/answer/176600
You also have another issue with that code that's un-related to the one you're subscribing. When setting up contact us forms, using the person's email as the FROM field will cause DMARC to fail. You can read up about that here
Also I don't understand why you are using the person's email address and password to connect to gmail with. You should use your own account if you're sending mail.

How to send emails from my website domain in asp.net

I want to send mail from my website domain(www.sample.com)
I have written below code. It is not returning any error. But it is not sending mail once I uploaded the pages in the server. Once click on email send, it is not getting any error, but not receiving the mail.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("mail.sample.co.uk", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("smtpuser#sample.co.uk", "pass#123");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//Setting From , To and CC
mail.From = new System.Net.Mail.MailAddress("smtpuser#sample.co.uk", "MyWeb Site");
mail.To.Add(new System.Net.Mail.MailAddress("myemail#gmail.com"));
mail.CC.Add(new System.Net.Mail.MailAddress("myemail1#gmail.com"));
smtpClient.Send(mail);
I am getting the "mailsend" response text in the page once calling the sending method.
Please help
Anjana
The following code works perfect for me. Make sure you have added a mail account on the smtp server and activated it.
private static string EMAIL_SERVER = "mailservername.mydomain.com";
private static string EMAIL_NAME = "myMailAccount#mydomain.com";
private static string EMAIL_PASSWORD = "complex_password123";
private static string EMAIL_SEND_TO = "sample#mail.com";
protected string SendMail()
{
try
{
System.Net.Mail.MailMessage objMM = new System.Net.Mail.MailMessage();
objMM.From = new MailAddress("fromMe#mail.com", "john doe");
objMM.To.Add(new MailAddress(EMAIL_SEND_TO)); //Note: this To a collection
objMM.Subject = "Subject1";
objMM.Body = "Hello world this is my text";
objMM.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(EMAIL_SERVER);
smtp.Credentials = new NetworkCredential(EMAIL_NAME, EMAIL_PASSWORD);
smtp.Send(objMM);
}
catch (Exception e)
{
return "Message can not be send couse of error: " + e.ToString();
}
return "Message is send.";
}

Tracking Bounced Emails Through ASP.Net

Is there any way I can track (through the code) the bounced emails.
Consider the email id like 'skdfisdcnsodioifs#gmail.com'. This is valid email id but does not exists so certainly it will be bounced backed.
I am using ASP.Net's SmtpClient with "message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure". However it still shows the message sent successfully and after ~20-25 minutes I get the fail delivery notification email.
Below is my code
class Program
{
static void Main(string[] args)
{
SmtpClient client = new SmtpClient("xxxx.xxxxxx.xxx", 25);
client.Credentials = new NetworkCredential("xxxxxx#xxxxxx.com", "xxxxxx");
MailAddress to = new MailAddress("abcdsfasdfasdfasdfasdf2342342defgh#gmail12333.com");
MailAddress from = new MailAddress("xxxxxx#xxxxxx.com");
MailMessage message = new MailMessage(from, to);
message.Headers.Add("Return-Path", "xxxxxx#gmail.com");
message.ReplyToList.Add(new MailAddress("xxxxxx#gmail.com"));
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess;
message.Subject = "Test POC";
message.Body = "This is a test e-mail message sent by an application. ";
client.SendCompleted += client_SendCompleted;
string UserState = "test";
client.Timeout = 3;
try
{
Console.WriteLine("start to send email ...");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.SendAsync(message,UserState);
Console.WriteLine("email was sent successfully!");
}
catch (SmtpFailedRecipientsException ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
Console.WriteLine("test.");
Console.ReadKey();
}
static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
//String token = (string)e.UserState;
if (e.Cancelled)
{
Console.WriteLine("Send canceled.");
}
if (e.Error != null)
{
Console.WriteLine("[{0}] ", e.Error.ToString());
}
else
{
Console.WriteLine("Message sent.");
}
}
}
Thanks in advance.
Abhishek

Error in sending email through asp.net

I'm currently working on a asp.net website using Visual Studio 2010... I'm trying to send email from my contact us page... I'm getting this error:
Warning 9 CA2000 : Microsoft.Reliability :
In method 'Default2.Button1_Click(object, EventArgs)',
call System.IDisposable.Dispose on object 'msg'
before all references to it are out of scope.
c:\Users\toshiba\Documents\Visual Studio 2010\WebSites\Carp-MacDental\ContactUs.aspx.cs 29
C:\...\Carp-MacDental\
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class Default2 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("Site#gmail.com", "password");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("Site#gmail.com");
msg.To.Add(new MailAddress(txtEmail.Text));
msg.Bcc.Add(new MailAddress("Site#gmail.com"));
msg.Subject = "Notification from:" + url;
msg.Body = "A message form," + txtName.Text + ", <br/>" + txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
lblMessage.Text = "Thank you for contacting us.. we will get back to you as soon as we read your message";
}
}
Its my first time working with this so I'm pretty confused.. Also my mail message that was supposed to be sent through the website wasn't sent...
As Aristos indicated with his link to Msdn, the warning you see is a code analysis warning. Visual Studio has identified that there is a small problem with your code and shows the warning to alert you about the problem.
The problem is that you are creating a couple of instances of classes that implement the IDisposable interface. Classes that implement this interface have a Dispose method that should be called when you are finished with the instance. The Dispose method is used to clean up unmanaged resources that the instance is using behind the scene, thus making sure that memory and other unmanaged resources are freed up for use by other processes.
Both MailMessage and SmtpClient implement this interface. So intances of these classes should have their respective Dispose method called before they go out of scope. The first thought might be to simply add a couple of lines to you existing code:
...
client.Send(msg);
msg.Dispose();
client.Dispose();
...
But a better soultion would probably be to wrap these in using() { } blocks. Doing this will make the compiler insert code that automatically calls the Dispose method, and it will even be called even if there is an exception thrown from inside the using() { } block. Try this:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("Site#gmail.com", "password");
using(MailMessage msg = new MailMessage())
using(SmtpClient client = new SmtpClient("smtp.gmail.com"))
{
msg.From = new MailAddress("Site#gmail.com");
msg.To.Add(new MailAddress(txtEmail.Text));
msg.Bcc.Add(new MailAddress("Site#gmail.com"));
msg.Subject = "Notification from:" + url;
msg.Body = "A message form," + txtName.Text + ", <br/>" + txtMessage.Text;
msg.IsBodyHtml = true;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
lblMessage.Text = "Thank you for contacting us.. we will get back to you as soon as we read your message";
}
That should hopefully get rid of the compiler warning for you. The fact that the email is not really sent is another matter. I cannot help with that without a lot more details. If you need help with that, I suggest you post a new question and add as much info about the problem as possible in that question.

ASP.NET won't send email and no error message, weird!

I tried to send an email using this class below, but no success, no error message, the page just executed very fast, any problem with this class?
public bool mailSender(string strSubject, string strFrom, string strFromName, string strTo, string strBody)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(strFrom, strFromName);
smtpClient.Host = ConfigurationManager.AppSettings["smtpServer"];
smtpClient.Port = 25;
smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUsername"], ConfigurationManager.AppSettings["smtpPassword"]);
message.From = fromAddress;
message.To.Add(strTo);
message.Subject = strSubject;
message.IsBodyHtml = false;
message.Body = strBody;
smtpClient.Send(message);
return true;
}
catch
{
return false;
}
}
Your try/catch block is deliberately throwning away any error message. Remove that and see what you get.
piggy backing of what bruce said, do this:
try
'your code here'
catch ex As Exception
Response.Write(ex.Message)
end try
One thing I have noticed, especially when running in the debugger, is that the SmtpClient doesn't seem to actually send the mail until it gets disposed. At least, I often see the messages going out when I shutdown the debugger rather than at the time the mail is actually supposed to be sent.

Resources