Sending mails from WCF application via smtp-relay - c#

I'm not sure if this is the correct place to ask this question. But in my solution we have encountered a problem with sending mails.
Tech details:
I telnet the smtp-relay and get a response.
We send some emails from the application and it works.
And after a while the messages does not get sent and I can't telnet the smtp-server.
Then we need to restart the server and it works for a while.
The code that sends the message is:
protected virtual void Send(Email mailMessage)
{
using (SmtpClient client = new SmtpClient(this._mailService.SmtpContext.SmtpServer, this._mailService.SmtpContext.SmtpPort))
{
client.EnableSsl = _mailService.SmtpContext.EnableSsl;
if (_mailService.SmtpContext.UseDefaultCredentials)
{
client.UseDefaultCredentials = true;
}
else
{
NetworkCredential credentials = new NetworkCredential(this._mailService.SmtpContext.Username, this._mailService.SmtpContext.Password);
client.Credentials = credentials;
}
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(mailMessage);
}
catch (SmtpException e)
{
/// LOG
}
/// Trigger mail sent event
onMailSent(this, mailMessage);
}
}
I would like to know if anyone has faced an issue like this, and if there exist a solution.
We can see a connection being created in netstat, but it doesn't get removed. Is it some kind of error in the smptclient disposal?

Related

C# SMTP issue "Client was not authenticated"

I'm currently running into an issue that my code fails to authenticate against an Exchange server, where it runs fine with gmail.
Here's my code
client = new SmtpClient(smtp_server, smtp_port); //webmail.airbaltic.com
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = smpt_enableSSL;
client.Timeout = 60 * 5 * 1000;
client.UseDefaultCredentials = false;
if (string.IsNullOrEmpty(smtp_domain))
{
client.Credentials = new NetworkCredential(smtp_user, smtp_password);
}
else
client.Credentials = new NetworkCredential(smtp_user, smtp_password, smtp_domain);
try
{
using (MailMessage mail = new MailMessage(mail_from, message.recep))
{
mail.Subject = mail_title;
mail.Body = message.message;
if (message.attachedFile != null)
mail.Attachments.Add(message.attachedFile);
client.Send(mail);
if (message.attachedFile != null)
message.attachedFile.Dispose();
}
}
catch (Exception e)
{
Debug.LogError(e);
}
And the response I'm getting back from the server is "530 5.7.1 Client was not authenticated". I've quintuple checked the password and username, as well as the domain and server adress, scoured the forums, even tried the same configuration from an external mail client (where it works), and nothing.
The server itself doesn't use SSL and uses NTLM for authentication.
EDIT:
Looking into this further (i.e. checking sent and recieved packets), it seems like the client I used for testing the username and password does authentication first, while SmtpClient.send() seems to skip it for some reason

C# smtp.google.com could not be resolved

Following code used to work but suddenly refuses to work.
private static void SendMail()
{
try
{
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.google.com", 587);
mail.From = new MailAddress("catthoor.jc#gmail.com", "Jasper.Kattoor");
mail.To.Add("YYYY");
mail.Subject = "sup";
mail.Body = "sup";
smtpServer.Credentials = new NetworkCredential("catthoor.jc#gmail.com", "XXXX");
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
I receive the following error:
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: The remote name could not be resolved:
'smtp.google.com'
I've also tried using hotmail instead of gmail, same error.
I can still send mails manually though.
Why would this error suddenly occur? Yesterday there were no problems with this.
That remote host name is wrong, it should be:
smtp.gmail.com
Read all about it: Send Email from Yahoo!, GMail, Hotmail (C#)
Updates: You can also ping the host name to check if it exists using command prompt
Yes, in my case I wasn't just connected to the internet.
After I connected the problem was gone.

Closing Transmission Error. Timeout waiting for the data from client

I am using local host to send bulk mails through SES. This question is answered by many but none of the solutions is helping me. The problem is I could send 100/ 150 mails at a time after that the above error is showing up. I tried to dispose of the client as suggested by some, but not working. I am using C# code to do this. Any answers/ suggestions is much appreciated. The below is the code I am using to send bulk mail using for loop. You might be thinking it might be a throttling issue, it is not because we have 70 emails/second and 500000 emails per day.
Parallel.For(0, mail.Count, i =>
{
// Replace with your "From" address. This address must be verified.
String TO = mail; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
//Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
//the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.Send(message1);
client.Dispose();
}
});
I don't know the exact reason why it is working now, but it's working. I changed the logic of the above code, it started working. Instead of fetching the SMTP connection each time, for sending each mail previously, this time I fetched the smtp connection only once and used it to send all the bulk mails at once and it started working.But the problem is the sending time, it is taking too much to send all the mails.Anyways I will find the solution for this also.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
client.EnableSsl = true;
for(i=0;i<mail.Count;i++)
{
String TO = mail[i];
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message1);
}
client.Dispose();
}
Label1.Text = mail.Count.ToString() + " mails sent !!";

SmtpDeliveryMethod.PickupDirectoryFromIis strange behavior

I think i need some guru lights!
public void SendEndingMail(string fileName)
{
SmtpClient client;
client = new SmtpClient("smtp.myserver.com", 25);
//client = new SmtpClient();
if (!string.IsNullOrEmpty(""))
{
System.Net.NetworkCredential credential = new NetworkCredential("", "");
client.Credentials = credential;
}
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
MailAddress fromAddress = new MailAddress("mailing#mydom.com", "Elec");
MailAddress toAdrress = new MailAddress("mailing#mydom.com");
using (System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(fromAddress, toAdrress))
{
mailMessage.Attachments.Add(new System.Net.Mail.Attachment(fileName));
mailMessage.IsBodyHtml = false;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
try
{
client.Send(mailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
Is that true that:
when i set
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
It does not matter whichever smtp server i use
client = new SmtpClient("smtp.myserver.com", 25);
//client = new SmtpClient();
The both lines are the same since it will use LOCAL IIS ?!!!
Is this is true, it is not normal that the API is build this way!? it is very confusing...
Thanks
Jonathan
IIRC, when the SmtpClient sends the email, it looks at the .DeliveryMethod value. If the value is Network, then it sends via network. If it is PickupDirectoryFromIis, then it ignores any specified SMTP server (because it just writes and the email to the filesystem), and writes it to the Pickup directory. No network communication takes place.
That is a bug in the Send routine - it creates an smtp server object even if one isn't specified, and when it later (after Send) tries to dispose it, it throws an exception. This happens AFTER the mail is successfully placed in the pickup directory, so the mail will be sent.
Workarounds:
Specify localhost as SMTP server. It won't be used, but prevents the exception.
A blind try/catch around the Send method (BAD solution).

How to send the mail from c#

I have code,
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(oMailMessage);
(all variables have values)
I have installed SMTP virtual services. why it is unable to send emails. why it is not working ??
EDIT
public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody)
{
try
{
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "SERVERNAME";
SmtpMail.Send(oMailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
I have this code. It is executing fine and is returning true, but I'm not getting any email in the inbox.
What else could be wrong?
Getting some mails in BadMail Dir at C:\Inetpub\mailroot\Badmail also in Queue Directory getting some mails here ... what does that means..??
I found that mail only can sent to gmail accounts... why it is?
As mentioned by others, your code is fine and is most likely something in your SMTP configuration or maybe your email client your sending your test emails to is marking them as spam. If it's spam, well that's easy enoughto figure out.
If it's something with the email, you can go to your mailroot folder and their will be some folders there with the email files along with a description. See if there's anything in the BadMail folder or the queue folder and open them up in notepad and view what error is given for why they weren't sent.
Determine what the error is:
try
{
SmtpMail.Send(oMailMessage);
}
catch (Exception ex)
{
//breakpoint here to determine what the error is:
Console.WriteLine(ex.Message);
}
From here, please edit your question with that exception details.
Its hard to tell, but one possibility is that you haven't enabled anonymous access on the SMTP virtual server. Go to the the virtual server properties dialog, select the Access tab, click the Access Control button, and make sure that Anonymous Access is enabled.
There doesn't appear to be anything functionally wrong with your program. It's likely a configuration issue between your program and the mail server. I would try the following to diagnose the problem.
Wrap the code in a try/catch block and see if the exception message contains useful data
Use 127.0.0.1 instead of localhost just to rule out anything crazy
Ensure your SMTP server is running on the standard port (25 I believe)
Hello you can follow the following code:
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your gmail id", "password");
MailMessage msg = new MailMessage();
msg.To.Add(textBoxTo.Text);
msg.From = new MailAddress("your gmail id");
msg.Subject = textBoxSubject.Text;
msg.Body = textBoxMsg.Text;
Attachment data = new Attachment(textBoxAttachment.Text);
msg.Attachments.Add(data);
client.Send(msg);
MessageBox.Show("Successfully Sent Message.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
have you tried 127.0.0.1 instead of Localhost?
Also have you tested that the SMTP service is working, check out this link for details.
In the virtual smtp server Add relay restrictions and connection control so that none of the outside connections are allowed

Categories

Resources