I am using #jstedfast Mimekit/Mailkit library to send emails from my API. I want to know how to get the delivery status of each email. I tried overriding DeliveryStatusNotification to send notifications on Success:
public class DSNSmtpClient : SmtpClient
{
public DSNSmtpClient()
{
}
protected override string GetEnvelopeId(MimeMessage message)
{
return message.MessageId;
}
protected override DeliveryStatusNotification? GetDeliveryStatusNotifications(MimeMessage message, MailboxAddress mailbox)
{
return DeliveryStatusNotification.Success;
}
}
I am able to send emails like this:
using (DSNSmtpClient dsnSmtpClient = new DSNSmtpClient())
{
dsnSmtpClient.Connect(_emailCredentials.Value.SmtpServer, _emailCredentials.Value.Port, true);
dsnSmtpClient.Authenticate(_emailCredentials.Value.UserName, _emailCredentials.Value.Password);
dsnSmtpClient.Send(mimeMessage);
dsnSmtpClient.Disconnect(true);
}
But I do not get any emails regarding delivery status(or anything) in my sender inbox.
I found some related links:
get the delivery status of email with mimekit/mailkit library
https://github.com/jstedfast/MailKit/issues/602
But they only got me this far.
What else do I need to do to see if an email was delivered or not?
Make sure that your SMTP server supports the DSN extension.
var supportsDsn = client.Capabilities.HasFlag (SmtpCapabilities.Dsn);
Related
This is my method for building the e-mail object and sending it:
public async Task SendEmail(List<ApplicationUser> bccUsers, string subject, string contentText, string contentHtml)
{
if (bccUsers.Count > 0)
{
var client = new SendGridClient("MyClientKey");
var from = new EmailAddress("noreply#domain", "MyProjectName");
var to = new EmailAddress("noreply#domain", "MyProjectName");
var msg = MailHelper.CreateSingleEmail(from, to, subject, contentText, contentHtml);
foreach (ApplicationUser user in bccUsers)
{
msg.AddBcc(user.Email, $"{user.FirstName} {user.LastName}");
}
await client.SendEmailAsync(msg);
}
}
I have confirmed that all the e-mail addresses are receiving other e-mails.
When inspecting the msg-object, all the BCC recipients are there:
What am I doing wrong?
Update
I don't know if this is relevant info, but all the three e-mail addresses are being forwarded to my gmail-address. I am only checking emails there.
One of those addresses is in use, and I am receiving emails to it regularly.
The other two addresses I have just created for testing purposes. They do not have mailboxes of their own at the hosting company, only forwarding to my gmail address.
I have an mvc application that uses gmail to send out an email.
My code:
Successfully sends out an email, providing:
I use proper account authentication (username, pw)
I send the email to a valid existing email account
I get an exception if I do not use my valid gmail username, pw. (which is good).
My question and my problem is:
When my code sends an email to an invalid email account, I do not get an exception of any kind! No SmtpFailedRecipientException, no SmtpFailedRecipientsException; nada.
Why is this happening? What do I need to change so that I get an exception back when an email is sent to an invalid, non-existent account?
Code in my MVC app:
In Identity.cs
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
try
{
System.Web.Helpers.WebMail.Send(message.Destination, message.Subject, message.Body, "DoNotReply#ourcompany.com");
}
catch (Exception e)
{
throw; //Add msg here; set break point when testing; whatever
}
return Task.FromResult(0);
}
}
And in AuthConfig.cs, I have the gmail account settings, which we know work.
System.Web.Helpers.WebMail.SmtpServer = "smtp.gmail.com";
WebMail.EnableSsl = true;
WebMail.UserName = "OurUserName";
WebMail.Password = "OurPW";
WebMail.From = "DoNotReply#OurCompany.com";
WebMail.SmtpPort = 587;
In my controller, I have this partial code:
public async Task<ActionResult> Register(RegisterViewModel model)
{
try
{
await UserManager.SendEmailAsync(user.Id, emailSubject, emailBody);
}
catch (Exception e)
{
}
}
The controller runs first. I am using the asp.identity2 (UserManager) and SendEmailAsync calls the SendAsync in Identity.cs.
Thank you for suggestions.
Gmail doesn't let you know which email addresses it has failed to send emails to to avoid bots looking for valid email addresses. You should get an email later though with details about failed email addresses.
I'm trying get unread mails from Gmail by IMAP protocol. I followed this tuto : https://briancaos.wordpress.com/2012/04/24/reading-mails-using-imap-and-mailsystem-net/#comment-4999 , which recieved many good comments.
But when I call
MessageCollection messages = mails.SearchParse("UNSEEN");
I got error message "index and length must refer to a location within the string".
I just call a simple function, so I don't know what's wrong. For more detail, here is my code snippet:
public class MailRepository
{
private Imap4Client _client = null;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get
{
if (_client == null)
_client = new Imap4Client();
return _client;
}
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
This error may occur when your mails contain non-ASCII characters. As I don't see an easy fix here and MailSystem.NET is no longer supported, I recommend using an alternative library.
Mailkit seems to be a good option. Also look here for further reference: https://stackoverflow.com/a/23375968
Recently somebody answered me on this site, that this method can send email from .net application:
public static void SendEmail(bool isHTML, string toEmail, string fromEmail, string subject, string message)
{
var sm = new SmtpClient("smtp.mail.ru");
sm.Credentials = new NetworkCredential("MyLogin", "MyPass");
var m = new MailMessage(fromEmail, toEmail) { Subject = subject, Body = message };
if (isHTML)
{
m.IsBodyHtml = true;
}
sm.Send(m); // SmtpException
}
It is true. But now I want to use this method from Asp.Net WebService, but I have SmtpException at last string. Why? And do I send email from web service.
So the problem is not with your code, rather the transaction with the SMTP server is failing for some reason. If you have access to the SMTP server, check its logs. Otherwise you might have to use a sniffer like WireShark to figure it out.
To verify this, you can try using a different mail server, assuming you have proper access to that server it should send the mail properly.
I am writing a small utility to help process some MySQL tasks every night and have it email my personal email if it fails (this is a personal project, so no company smtp server or anything, emails going through public outlook accounts).
I tested about 5 times and each send was successful, but now any attempts to send email I get this exception:
Error sending test email: Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:OutboundSpamException; Failed to process message due to a permanent exception with message WASCL UserAction verdict is not None. Actual verdict is Suspend, ShowTierUpgrade. OutboundSpamException: WASCL UserAction verdict is not None. Actual verdict is Suspend, ShowTierUpgrade.[Hostname=BY2PR0101MB1461.prod.exchangelabs.com]
A bit of an oops on my part - didn't think Outlook would consider it as spam on the 6th try - is there anything I can do in Outlook to correct this?
I am using a service account I created in outlook to send these emails to my personal inbox.
The actual code in question:
class JobMailer
{
private string email_to;
private string email_from;
private string password;
private string email_smtp;
private bool use_ssl;
private int port;
public void Send(string subject, string body)
{
MailMessage mail = new MailMessage(email_from, email_to);
using (SmtpClient client = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = use_ssl,
Host = email_smtp,
Timeout = 100000,
Port = port,
Credentials = new NetworkCredential(email_from, password)
})
{
mail.Subject = subject;
mail.Body = body;
client.Send(mail);
}
}
public JobMailer(string emailTo, string smtp, string emailFrom, string pw, int p, bool ssl)
{
email_to = emailTo;
email_from = emailFrom;
password = pw;
email_smtp = smtp;
port = p;
use_ssl = ssl;
}
}
I resolved this by verifying the account I was trying to use. Each time you encounter this error an email is sent to the account with instructions on what you need to do to resolve the error. Typically you will need to verify against a phone number.
Got this error trying to send lots of emails to myself at Outlook.com, using SMTP.
To fix it I simply added a 5 second delay between the sends, for example:
foreach(var mail in mailToSend)
{
await smtpClient.SendMailAsync(mail);
Console.WriteLine("Sent email: " + mail);
await Task.Delay(5000);
}
If you aren't doing this just as a test, then you can contact the Outlook.com team and ask them to whitelist your IP (make sure you have SPF, rDNS, etc. setup first).