asp.net mailmessage BCC and CC won't work - c#

Does anyone see anything wrong with this code:
MailMessage msg = new MailMessage();
msg.From = new MailAddress(WebConfigurationManager.AppSettings.Get("ReservationsFrom"));
msg.ReplyTo = new MailAddress(myRes.Email);
msg.To.Add(new MailAddress(WebConfigurationManager.AppSettings.Get("ReservationsTo")));
msg.CC.Add(new MailAddress(WebConfigurationManager.AppSettings.Get("ReservationsBcc")));
Try as I might, I can only get the 'To' address and the "ReplyTo" to work, the CC and the BCC never receive mail, even if I hard code the addresses in.
Am I missing something obvious here?
Edit: And yes, I am sure I am pulling the right addresses out of the web.config - like I said, even if I hard code a static address the BCC and CC never received email.

I had similar problem and checked the smtp log. Looks like .net sends only one message, if "To" and "Cc"/"Bcc" adress are same.

If static addresses hard-coded into the method calls aren't working, you're having a problem with delivery, not the addresses.
Can you telnet to port 25 on the smtp host you're using? Can you send an email to the test addresses from a regular email client (not web-based)?

Related

Does anyone know ow to fix this issue sending SMTP messages to Gmail accounts?

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.

.NET Mail - mail never hits exception if the address is invalid. - C#

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

C# e-mail issue

I have this help desk application which sends an e-mail to me with a specific issue. The user is required to input his e-mail address into a textbox. What I'd like to do is for the user to get a copy of the problem as well.
MailMessage req_mail = new MailMessage(reqMail.Text, "system.admin#home.com");
Where reqMail.Text is the user's e-mail address.
Any ideas why it doesn't work? Because I can see from who it is...
The overload of the MailMessage constructor you're using takes a from and a to address.
You've supplied the users email as the from and your email as to. AFAIK, there isn't a constructor for CC'ing or BCC'ing.
Instead you just need to do this:
req_mail.Bcc.Add(new MailAddress(reqMail.Text));
Or:
req_mail.CC.Add(new MailAddress(reqMail.Text));
If you prefer.
According to the constructor you're using that MailMessage is from the user, not to the user. If you want to send the message to the user then their address would need to be in the To or CC or BCC parts of the message. Something like:
var req_mail = new MailMessage(reqMail.Text, "system.admin#home.com");
req_mail.CC.Add(new MailAddress(reqMail.Text));

Sending email from web site using Google Apps account

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.

Sending emails on behalf of user

So im implementing a mailservice in our webapp that users can utilize to send out emails to persons on a certain list. Now i need to know how to best use the System.Net.Mail object to send mail on the users behalf. Ive been trying this now for a while without the right results.
I would like the mail to read "our system on behalf of user 1" and the reply to adress should be the adress that user1 has in our system so that when the contacted person wants to reply to the mail he should get user1:s address, not ours. How can I do this?
Which fields do I need to declare and how? This is how i have it set up right now, but a reply to these settings sends a mail back to noreply#oursystem.com
from = 'noreply#oursystem.com'
replyTo = 'user1#privateaddress.com'
to = 'user1#privateaddress.com'
sender = 'user1#privateaddress.com'
cc = ''
ReplyTo is obsolete. You should use ReplyToList
Example:
MailAddress mailFrom = new MailAddress("noreply#oursystem.com");
MailAddress mailTo = new MailAddress("user1#privateaddress.com");
MailAddress mailReplyTo = new MailAddress("user1#privateaddress.com");
MailMessage message = new MailMessage();
message.From = mailFrom;
message.To.Add(mailTo); //here you could add multiple recepients
message.ReplyToList.Add(mailReplyTo); //here you could add multiple replyTo adresses
This was caused by a programming error on the receiving end. The correct solution is to set the from and replyto as above. That will get the correct behaviour.

Categories

Resources