Set Deliverynotification and Readnotification for outgoing Mails - c#

I send invoices and warnings/reminders to customers and now these mails should be flagged with a Deliverynotification and Readnotification. So, I know when customers received and read the invoice.
MailMessage email = new MailMessage();
email.Priority = MailPriority.Normal;
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; // Deliverynotification?
email.Attachments.Add(new Attachment(docDunPath));
email.Attachments.Add(new Attachment(docInvoicePath));
email.Subject = "-----------";
email.IsBodyHtml = true;
email.Body = MailText;
Everything works so far, I just want to set the options for an automatic response when the recipient has read the mail. So, I can be sure he saw his invoice.

So after realising i used System.Net.Mail i switched to Microsoft.Exchange.WebServices and it works perfectly now.
EmailMessage email = new EmailMessage(service);
email.IsDeliveryReceiptRequested = true;
email.IsReadReceiptRequested = true;

Related

C#-mailmessage: get Delivery Status Notification to different email address

Is it possible to set a different email to receive the Delivery Status Notification in C#? I tried setting the ReplyToList but didn't work as I wanted.
I use gmail provider.
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress("myacc#gmail.com");
emailMessage.To.Add(new MailAddress("accto#gmail.com"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587);
MailClient.EnableSsl = true;
MailClient.Credentials = new System.Net.NetworkCredential("myacc#gmail.com", "mypass");
MailClient.Send(emailMessage);
There is no a clear answer if such header exists or not in other questions.
The way it's commonly claimed to achieve this is
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOPtions.OnFailure;
mailMessage.AddHeaderField( "Return-Receipt-To" , "\"John Doe\" <johnDoe#blahblahblah.com>" );
mailMessage.AddHeaderField( "Disposition-Notification-To" , "\"John Doe\" <johnDoe#blahblahblah.com>" );
But there is no guarantee the request will be honored by the mail servers or the recipient.
The Disposition-Notification-To header field is the standard way, and Return-Receipt-To is non-standard.

using gmail as smpt for deliver email from mvc controller

I'm trying to send email from asp.net mvc controller. Gmail account used here for smpt is configured to use with less security, so that's not the problem here.
but I don't get any error message neither any exception, but it not
deliver at my expected email address.
I'm using code
var text = "email body to deliver";
SendEmail("mydeliverEmailAddress#gmail.com", text);
public static bool SendEmail(string SentTo, string Text)
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("myemail#gmail.com", "myGmailPass");
client.Port = 465;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
MailAddress
maFrom = new MailAddress("sender_email#domain.tld", "Sender's Name", Encoding.UTF8),
maTo = new MailAddress(SentTo, "Recipient's Name", Encoding.UTF8);
MailMessage mmsg = new MailMessage(maFrom.Address, maTo.Address);
mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
mmsg.IsBodyHtml = true;
mmsg.Subject = "Some Other Text as Subject";
mmsg.SubjectEncoding = Encoding.UTF8;
client.Send(mmsg);
}
catch (Exception ex)
{
}
return true;
}
Wait a minute. You are using your gmail account: myemail#gmail.com and trying to send an email on behalf of sender_email#domain.tld?
For more than obvious reasons that's never gonna work. So make sure that you are using the same email address as the one you are authenticating against:
maFrom = new MailAddress("myemail#gmail.com", "Sender's Name", Encoding.UTF8),
You can only send emails from the account you are authenticated against. Of course the recipient email can be any address that gmail can deliver to.
You've got another issue with your code. You are using a wring port here:
client.Port = 465;
The correct port that gmail SMTP works with is the following:
client.Port = 587;
Also you might want to ensure that you have enabled less secure apps in your gmail account or you will not be able to use SmtpClient in .NET to send emails using this SMTP: https://www.google.com/settings/security/lesssecureapps?pli=1
but I don't get any error message neither any exception, but it not
deliver at my expected email address.
What error message do you expect to get when you did the worst ever possible thing? You wrapped your code in a try/catch block and in your catch block you did absolutely nothing. You just consumed the exception:
catch (Exception ex)
{
}
So make sure that you do something useful with an exception if you are going to be catching it. For example something useful could be to log this exception and send an error message to the user saying that something bad happened and you couldn't send an email and that you are investigating the issue right now.
var smtpClient = new SmtpClient("YourSMTPServer", "SMTPServerPort"))
{
Credentials = new NetworkCredential("YourEmail",
"Password"),
EnableSsl = false
};
string fromEmail = "YourEmail";
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromEmail);
mailMessage.To.Add("Recipient's EMail");
mailMessage.Subject = "Test Mail";
mailMessage.Body = "This is test Mail";
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);

Mail body: & becomes (R)

We have a site from where we send a auto generated mail to our customer when a new customer register. our mail got a activation link which look like
http://www.bba-reman.com/catalogue/ConfirmRegistration.aspx?email=meyer#reman.de&id=907a5253-106c-4fb3-9882-83e634e651b2
but when our german customer reveive the mail then he got the below activation link where you notice & character change to ®
http://www.bba-reman.com/catalogue/ConfirmRegistration.aspx?email=meyer#reman.de®id=907a5253-106c-4fb3-9882-83e634e651b2
Can anyone tell me why & character getting change to ®?
How to resolve this kind of problem?
try using CreateAlternateViewFromString property, here is the example code
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);

What Address should I give in From field in SendEmail using c#

I caught into big problem since am new to SMTP Email Server.
I have installed smtp server in my web server and configured the required details. My emails are now sending but in spam
I have implemented SendMail code using c# in my webapplication and I have one clarification on that.
string mailFrom = "Newsletter#my-domain.com";
string message = string.Empty;
System.Net.Mail.MailMessage email = new MailMessage(mailFrom, EmailAddress);
email.Subject = "Mail from my-domain.com";
email.Body = message;
email.IsBodyHtml = true;
email.Priority = MailPriority.High;
System.Net.Mail.SmtpClient mailClient = new SmtpClient();
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("username", "password");
mailClient.Host = "my-mail-server-domain.com";
mailClient.Port = 25;
mailClient.EnableSsl = false;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
try
{
mailClient.Send(email);
}
catch (Exception ex)
{
log4net.ILog logger = log4net.LogManager.GetLogger("File");
logger.Error(ex.ToString());
}
What value should I give for mailFrom. Is valid email is required for From address? Does it cause sending mail as spam? I don't have emailid in the name of newsletter#my-domain.com. What shall I do for that?
Please anyone clarify on this.
It is the responsibility of the mail server the validation of the address, all could be good or bad for c#. I think your focus is wrong.
The from should be a valid email address. That is the email from where an email is sent. The network credentials that you specify must match with the from address. Once you provide a valid email address, the emails will not go to spam folder hopefully.

EWS Managed API: how to set From of email?

I'm using EWS Managed API to sending email. Account "account#domain.com" have permissions "Send as" to use "sender#domain.com" mailbox to send messages (from Outlook, it's work fine).
But I try from code - it's not work, in mail i'm read in the field "From" "account#domain.com".
....
EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.From = txtFrom;
....
message.SendAndSaveCopy();
How to make sending mail on behalf of another user? :)
It's been a while since I fiddled with the same thing, and I concluded that it isn't possible, in spite of having "Send as" rights.
Impersonation is the only way to go with EWS, see MSDN:
ExchangeService service = new ExchangeService();
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("app#domain.com");
// impersonate user e.g. by specifying an SMTP address:
service.ImpersonatedUserId = new ImpersonatedUserId(
ConnectingIdType.SmtpAddress, "user#domain.com");
If impersonation isn't enabled, you'll have to supply the credentials of the user on behalf of whom you want to act. See this MSDN article.
ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("user", "password", "domain");
service.AutodiscoverUrl("user#domain.com");
Alternatively you can simply specify a reply-to address.
EmailMessage mail = new EmailMessage(service);
mail.ReplyTo.Add("user#email.com");
However, "Send as" rights do apply when sending mail using System.Net.Mail, which in many cases will do just fine when just sending e-mails. There are tons of examples illustrating how to do this.
// create new e-mail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("user#domain.com");
mail.To.Add(new MailAdress("recipient#somewhere.com"));
message.Subject = "Subject of e-mail";
message.Body = "Content of e-mail";
// send through SMTP server as specified in the config file
SmtpClient client = new SmtpClient();
client.Send(mail);
i think you should use the Sender property so the your code should look like:
EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.Sender= txtFrom;
....
message.SendAndSaveCopy();

Categories

Resources