I know there are various thread out there related to this problem but i was unable to take any of the responses on those thread and make it work on my server.
So let try to see if someone can help me out here.
99% of the emails go out properly and few actually return with that error.
My code looks like this
MailMessage mm = new MailMessage(Settings.EmailCustomerService, to, subject, body);
mm.SubjectEncoding = Encoding.UTF8;
mm.BodyEncoding = Encoding.UTF8;
mm.IsBodyHtml = true;
MailAddress add = new MailAddress(Settings.EmailCustomerService, "Customer Service");
mm.From = add;
try
{
SmtpClient client = new SmtpClient(Settings.EmailSMTP);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(Settings.EmailUser, Settings.EmailPwd);
System.Threading.ParameterizedThreadStart threadStart = new System.Threading.ParameterizedThreadStart(SendInThread);
threadStart.Invoke(new SendInThreadParams
{
client = client,
Message = mm
});
}
finally
{
mm = null;
}
Actually the Credentials code was added later but my code was run OK even without it. It just happen that 1% of the email never make it to the recipients and adding those 2 lines for Credentials did not make a difference.
The Settings.EmailUser is just a user on the server where the SMTP runs, but i have NOT attach it to nowhere.
I bet that's the problem.
The SMTP Server Relay is set to use 127.0.0.1 and the FQDN is just the name of the machine (something like "Machine1" ...nothing with a domain.com name)
The error I'm getting is this
Reporting-MTA: dns;Machine1
Received-From-MTA: dns;Machine1
Arrival-Date: Wed, 30 May 2012 23:08:36 -0700
Final-Recipient: rfc822;test#email.net
Action: failed
Status: 5.5.0
Diagnostic-Code: smtp;550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1)
Return message emailed back was:
> This is an automatically generated Delivery Status Notification.
Delivery to the following recipients failed.
test#email.com
Thanks in advanced...
In addition to the message/delivery-status attachment, the DSN will usually have the returned message. For this sort of issue you should post the headers of the returned message and the DSN as well.
It looks to me like your server has accepted the message, but has an error transmitting it onwards. If your server had rejected it, your code would have thrown an exception. So your server Machine1 accepted it, attempted to transmit it to email.net, but email.net rejected it. Machine1 then generated a DSN (delivery status notification, in your case an NDR = Non-Delivery Report).
In other words it is a configuration error with the email server not a code problem. Almost certainly the issue is that the email server is not set up with an FQDN as you stated.
As a configuration problem, it belongs on ServerFault.
Based on BEN answer I realized that I was missing the PRIMARY DND SUFFIX.
Mainly in order to find out your FQDN, you need to simply:
1) Open a Command Prompt
2) Type "ipconfig /all"
Read your HOST NAME + PRIMARY DNS SUFFIX.
My DNS SUFFIX was emtpy so i went and added using this link
http://www.simpledns.com/kb.aspx?kbid=1227
And then rebooted the machine.
Now the code works like a charm.
Thanks BEN !!!
Related
I am having an issue with sending emails specifically to Gmail-related accounts, and I'll be darned if I know what the issue is. This is a C# ASP.NET project, by the way.
First, the following code works, as long as I am sending to any email account OTHER than a Gmail account:
var mail = new MailMessage {
Subject = "test email",
Body = "this is only a test",
Priority = MailPriority.High,
IsBodyHtml = true,
From = new MailAddress ( "<outbound email here>" )
};
var msgID = Guid.NewGuid().ToString();
var sentBy="<outbound mail domain>";
mail.Headers.Add ( "message-id", $"<{msgID}>");
mail.Headers.Add ( "msg-id", $"<{msgID}#{sentBy}>");
mail.To.Add ( new MailAddress ( "<recipient email>" ) );
var smtpClient = new SmtpClient("<email server address>") {
Port = 587,
Credentials = new NetworkCredential("<sender's email address>", "<password>"),
};
smtpClient.Send ( mail );
I have removed email addresses and network credentials, obviously.
The code works, because as long as I send email to a NON-Gmail account, it comes through just fine. But anything going to a Gmail-related account never arrives.
I added the two lines in the code above to add a message ID to the header based on what I read in several older posts here about some mail servers, like Gmail, rejecting email messages that didn't include them, but it has not fixed the issue, and I'm out of ideas. My ISP says the SPF record for the mail server is fine, so according to them that's not the issue.
Has anyone else encountered this recently, and if so, how did you fix it?
To clarify, the comments/answers I have received so far are appreciated, but as I stated in the OP, this is a problem with sending emails TO Gmail accounts. I am using my ISP's mail server to send them, and I am adding a message ID to the header to address what the log says, that the message is missing a message ID and won't be accepted. I can send emails to other non Gmail accounts just fine, and when I inspect the headers they show a message id. So I don't know why this continues to be an issue.
So the answer to this is that the issue was related to changes Google made to policies for sending emails to Gmail accounts, which might require adjustments to the SPF record on the sending SMTP server. That was the case in this situation - the hosting company was slow to respond (took them more than a week) and had to elevate the issue to their Tier 3 techs, but once the SPF record was fixed to account for Google's changes, all was resolved.
Read multiple posts about .net mail delivery notifications and tested multiple things:
Trying to send mail via SMTP. No mails arrives and no exception error
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception.aspx
How to check MailMessage was delivered in .NET?
https://www.emailarchitect.net/easendmail/sdk/html/o_deliverynotificationoptions.htm
http://forums.asp.net/p/1189023/2038523.aspx
Though I'm still stuck. What I want to accomplish is the following: I only want to know if the email-address which the mail is send to is valid, and not valid as in a valid format but valid as in: does the email address exist.
Sometimes a user will send an email to an address but by accident types 1 letter wrong, the mail is valid since it contains a domain and # but the mail address doesn't exist. The sender will think it has been send correctly since they didn't receive any notification.
I want to make sure the sender gets the undelivered notification.
When using code to send mails through Outlook for example you automatically get a undelivered message if the mail address doesn't exists, however with SMTP (I am) not so lucky. I tried it with this bit of code:
using (var msg = new MailMessage())
using (var client = new SmtpClient("spamfilter.mySpamFilter.com", 587))
{
msg.IsBodyHtml = true;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
msg.From = new MailAddress(FromAddress);
msg.To.Add(TO);
msg.Body = Body;
msg.Subject = Subject;
//Send mail
try
{
client.Send(msg);
}
catch (SmtpFailedRecipientsException ex)
{
throw ex;
}
}
For the To address I created an address that is 100% invalid. Though, the exception is never reached.
Can the spamfilter be the problem? For example, when I check the spamfilter I can see the messages have the refused send status for the invalid email addresses but the action status is allowed (so no spam). So it looks like the spamfilter never returns the exception back to the code.
Or is it just not possible what I want to achieve?
Spamfilter:
I dont think that you will get an error here because the To Method takes either an string or an MailAdress which will also takes an string as input.
So every Adress will be accepted here.
Have you tried to place some not allowd characters like ' " % or something like this?
I guess you will get an exception here.
Everything else will be forwared to the smtp and the he will take care of the delivery and not your C# Code.
Regards
Andre
Been researching this issue for quite a while, but have yet to find any solution.
I can send email from the website using my regular gmail account using smtp.gmail.com as my host and port 587.
My current problem is that there's no problem sending the mail. I no longer receive an error. However, the email is never sent. Anyone have any ideas?
Here's the code:
Config:
<smtp from="admin#domain.com">
<network host="smtp.gmail.com" password="password" userName="admin#domain.com" port="587"/>
</smtp>
Code:
public void Send() {
bool bDev = ConfigurationManager.AppSettings["dev"] == "true";
MailMessage oMsg = new MailMessage();
foreach (string sAddress in To) {
if (sAddress != "") oMsg.To.Add(sAddress);
}
oMsg.From = ((FromName == null) || (FromName == "")) ?
new MailAddress(From) :
new MailAddress(From, FromName);
oMsg.Subject = Subject;
oMsg.Body = Body.ToString();
oMsg.IsBodyHtml = true;
oMsg.BodyEncoding = Encoding.UTF8;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = (new int[] { 587, 465 }).Contains(smtp.Port);
smtp.Send(oMsg);
}
I added a second To address and resumed testing. I began to receive email to both accounts. Then after removing the second address, it started working.
I ran into a new issue where my reply-to address was using the From address. So, I added additional code to set the reply address to that of the person's email address from my form.
Code
if (!string.IsNullOrEmpty(ReplyToAddress))
oMsg.ReplyTo = new MailAddress(ReplyToAddress);
However, I ran into an issue with Gmail ignoring my defined reply-to address, and using my from address. Apparently this is by design. I couldn't find any useful information on how to override this setting, so instead I did a workaround that might prove to be helpful to anyone else having this issue.
I created a generic email address with gmail (website#gmail.com) and setup a filter. Any emails coming from admin#domain.com need to be redirected to admin#domain.com.
Now, when I run my tests, all the emails are going where they are supposed to go AND the reply to field works great.
Not the best solution as it involves me remembering to setup a new account every time I run into this issue, but it's a solution that works for the time being until a better alternative comes up.
Had exactly same issue, that no error happened but also no email was sent. Google sometimes blocks your account(usually during testing). Try logging in into gmail account and see what it says.
UPD1: also check your sent folder in gmail.
UPD2: Also make sure that "From" email is the same as your gmail email address.
I am able to ping smtp.mail.yahoo.com from my system but when i send email from following code using yahoo address it gives error transport failed to connect to server.
The same code successfully sends the email from gmail account.
I am using port 465 for yahoo.
MailMessage oMsg = new MailMessage();
oMsg.From = from.Text;
oMsg.To = to.Text;
oMsg.Subject = "Hi";
oMsg.BodyFormat = MailFormat.Html;
oMsg.Body = msg.Text;
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
WebProxy proxy = WebProxy.GetDefaultProxy();
if (proxy.Address != null)
{
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/urlproxyserver", proxy.Address.Host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/proxyserverport", proxy.Address.Port);
}
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",from.Text);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass.Text);
// ADD AN ATTACHMENT.
/* MailAttachment oAttch = new MailAttachment(path+ "\\Image.bmp", MailEncoding.Base64);
oMsg.Attachments.Add(oAttch);*/
SmtpMail.SmtpServer.Insert(0,host);
if (proxy.Address != null)
MessageBox.Show("Sending via proxy settings: " + proxy.Address.ToString());
try
{
SmtpMail.Send(oMsg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
oMsg = null;
Any ideas why this error occurs?
Being able (or not) to ping a host does not say anything about whether you will be able to connect to a particular service on it. For that, you need to try to actually establish a connection. (And of course, the fact that you can establish a connection does not necessarily imply that the service in question is working properly.)
Usually, it's a good idea to use telnet to try connecting to the remote host on the port in question. The syntax on the command line is simply telnet host.fqdn.example.com portnumber. This will tell you if there is anything at all at the other end of the pipe responding to connection attempts, which is a first step in determining where the problem is.
Second, it's usually a good idea to trim the code to the minimal version that exhibits the problematic behavior, and include the full code to show the problematic behavior. You are using a number of variables in your code which we really know nothing about.
Some ISPs block outgoing connections to the SMTP ports on hosts other than their own mail servers, to reduce the amount of outgoing spam. Maybe there is a typo in the value in host? Maybe you are inadvertantly using some unexpected MailMessage implementation? And so on.
That said, I would definitely first try to connect to the mail server in question manually, through a proxy if you are using one to connect using that code. If that doesn't work either, then your problem at least has nothing to do with the code in the question, and you can look elsewhere (in which case one possible candidate would be ISP filters; maybe they have a list of allowed external SMTP hosts and Yahoo's isn't on it?).
I have a small email application that lets a user build a message from checkboxes and then send the message as an email. I am trying to add an image to the email, i.e. a logo or signature. The application worked fine but as I was reseaching getting the images into the email, I found that I should be using System.Net.Mail instead of Interop. So I changed my email class to the code below. Now I'm not recieving the emails. I'm assumeing this is because the code is set-up for a server and I just want to run this on my local machine. This is just something I'm playing with to help me understand some concepts so real world use is not going to be a factor. I just want to be able to test my little program on my Outlook email account locally. My code is as follows...
using System;
using System.Net.Mail;
using System.Net.Mime;
namespace Email_Notifier
{
public class EmailSender:Notification
{
string emailRecipient = System.Configuration.ConfigurationManager.AppSettings ["emailRecipient"];
public void SendMail(string message)
{
try
{
string strMailContent = message;
string fromAddress = "MyApp#gmail.com";
string toAddress = emailRecipient;
string contentId = "image1";
string path = (#"C:Libraries/Pictures/Logo.gif");
MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
mailMessage.Subject = "Email Notification";
LinkedResource logo = new LinkedResource( path , MediaTypeNames.Image.Gif);
logo.ContentId = "Logo";
// HTML formatting for logo
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:Logo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);
mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("localhost");
mailSender.Send(mailMessage);
}
catch (Exception e)
{
Console.WriteLine("Problem with email execution. Exception caught: ", e);
}
return;
}
}
}
You specify mailSender = new SmtpClient("localhost");.
Have you set up an SMTP server on your local machine? If not, you will need to do so to use SmtpClient. Otherwise, specify a hostname other than localhost, perhaps using your Gmail account as specified here, bearing in mind you will need to configure it for authentication and SSL.
See also the documentation for SmtpClient.
There are also one or two other issues I can see with your code - but let's deal with these after getting simple mail sending working.
if your problem is that you don't have a smtp server you are talking to on your machine, you will see an exception rather than just silently not having your email delivered (which i believe is what you are seeing given that you did not list an exception, but not completely sure). it could be that you are successfully delivering to your local smtp server, but it is failing to send the mail on. if you have the iis smtp server installed and that is what you are trying to use to send the mail, you can see failed email messages in the subdirectories of C:\Inetpub\mailroot. there is a badmail subdirectory that should have failed deliveries.
if you are on windows 7, it doesn't support the iis smtp server. an alternative for having an smtp server on your machine that doesn't actually deliver the email but does show you everything that has come through for any email address is http://smtp4dev.codeplex.com/. even if i do have the iis smtp server available in my operating system, i prefer this for development needs.