How to check MailMessage was delivered in .NET? - asp.net

Is there a way to check if SmtpClient successfully delivered an email? SmtpClient.Send() does not appear to return anything other than an exception. So if the method completes is it safe to assume the email will be successfully sent to the appropriate email box? My code is below:
MailMessage emailMessage = new MailMessage();
emailMessage.Subject = SubjectText;
emailMessage.IsBodyHtml = IsBodyHtml;
emailMessage.Body = BodyText;
emailMessage.From = new MailAddress(Settings.Default.FromEmail,Settings.Default.FromDisplayName);
//add recipients
foreach (string recipientAddress in RecipientAddresses.Split(new char[{','},StringSplitOptions.RemoveEmptyEntries))
emailMessage.To.Add(recipientAddress);
using (SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Send(emailMessage);
}

No, there is no reliable way to find out if a message was indeed delivered.
Doing so will require access to the end SMTP server for every person you are emailing.
If you do not get an exception, you can assume that the SMTP server did its best to deliver the email.

There's no way to be 100% sure that a mail message has been received when sent via SmtpClient due to the way email works. The fact that SmtpClient doesn't throw an exception essentially means that you've done everything right, but a failure can happen further down the line, for example:
The receiving mail server could reject the mail
An intermediate mail server could reject the mail
The server that SmtpClient is transmitting mail through could decide to refuse to transmit the mail

One solution you could use is to create an httphandler for your website images. If you send an HTML message which includes at least 1 image, then you could embed querystring data to the end of that image. This could even be something like a 1x1 transparent image. When the user reads the email, this sends the request to the server to fetch the image data, and in turn, you could capture that request and denote that the message was read.
This is not bulletproof however, because most email clients block images by default unless the user specifies they would like to view images in the email.

If the recipient e-mail address is valid you don't get an immediate return value about the successful delivery of the message; see the signature:
public void Send(MailMessage message)
The SMTP server will notify the sender (or whoever you specify for the notification) almost immediately with an 'Undeliverable' notification whenever the recipient e-mail address is invalid/fake.
SMTP servers are required to periodically retry delivery. When the recipient e-mail address is a valid address but for some reason the SMTP server could not deliver the message, the SMTP server will return a failure message to the sender if it cannot deliver the message after a certain period of time.
RFC 2821 contains more details.
From section 2.1 Basic Structure
In other words, message transfer can occur in a single connection
between the original SMTP-sender and the final SMTP-recipient, or can
occur in a series of hops through intermediary systems. In either
case, a formal handoff of responsibility for the message occurs: the
protocol requires that a server accept responsibility for either
delivering a message or properly reporting the failure to do so.
See sections 4.5.4 and 4.5.5
From section 6.1 Reliable Delivery and Replies by Email
If there is a delivery failure after acceptance of a message, the
receiver-SMTP MUST formulate and mail a notification message. This
notification MUST be sent using a null ("<>") reverse path in the
envelope. The recipient of this notification MUST be the address from
the envelope return path (or the Return-Path: line).

Related

Transport username sent instead of sender's email | Symfony Mailer

I'am experiencing the issue of receiving the transport username (email) as the sender instead of the email written in the from() function when sending emails using Symfony Mailer.
Here is the code:
use Symfony\Component\Mailer\Bridge\Google\Transport\GmailSmtpTransport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
$transport = new GmailSmtpTransport($username, $password);
$mailer = new Mailer($transport);
$email = (new Email())
->from(new Address($data["email"]))
->to("dest#gmail.com")
->subject(sprintf("Apply for a %s.", ucfirst($data["subject"])))
->html($template);
$mailer->send($email);
Any idea how to fix it?
To fix this issue, I found the following solutions:
Use a different SMTP server that does not override the From header. You can try using a different email service provider or configuring your own SMTP server to avoid this issue.
Set the Sender header in your email message. This header specifies the email address that should be used as the actual sender of the message, while the From header specifies the name and email address of the person or entity sending the message. You can set the Sender header using the sender() method of the Symfony\Component\Mime\Email class.
For example:
use Symfony\Component\Mime\Email;
$email = (new Email())
->from(new Address($data["email"]))
->to('dest#gmail.com')
->subject(sprintf("Apply for a %s.", ucfirst($data["subject"])))
->html($template)
->sender(new Address($data["email"]));
Configure your SMTP server to allow sending emails with a different From address. This may require modifying the configuration of your email service provider or SMTP server to allow sending emails with a different From address. You can consult the documentation of your email service provider or SMTP server for more information on how to do this.
Note that some SMTP servers may still override the From header even if you set the Sender header or configure the server to allow sending emails with a different From address. In such cases, you may need to use a different SMTP server that does not override the From header.

How to set hostname used in JavaMail or Apache James Message-Id header?

I am having issues with mail bouncing when sending from my own server to my own active yahoo account using JavaMail. The mails are passing SPF, DKIM and DMARC according to google mail that receives the same messages being bounced by yahoo. I can send messages from other accounts to my yahoo account without issue.
The messages send fine from my server to ZMail, GMail, Microsoft mail. Looking at the emails, the only thing that I have noticed is the message header for the Message-Id. My messages have the following header:
Message-ID: <923936395.17.1634776639078#[internally visible hostname]>
I am wondering if this header could be the problem and whether there is a way in JavaMail or in the Apache James to set the hostname or IP address that gets used in this message so that rather than using the "internally visible hostname", I can get the hostname that is externally visible. I have been searching the available documentation for Apache James and JavaMail but have not found any parameters to try in order to resolve this.
According to the Decompiled SRC of sun mail it should be possible by setting some properties for your session.
props.setProperty("mail.from", user);
props.setProperty("mail.host", host);
//props.setProperty("mail.user", user);
The Id will be updated by the save method (saveChanges()) and will trigger an new ID generation (updateHeaders() -> updateMessageID()). (Looked up in the decompiled MimeMessage.class)
Leading to the HostPart called in javax.mail.internet.InternetAddress.
The relevant method is _getLocalAddress.
Here you can see that the values get extracted from the Properties or will fallback to your local machine.
Used Fields:
user.name
mail.from
mail.user
mail.host
The user.name property can also be looked up from the system props.

ax2009 x++ Error 0x80040211 in RunBaseBatch

I want to send email in RunBaseBatch, but I get error 0x80040211 (unknown).
I found in google that means:
The message could not be sent to the SMTP server.
The transport error code was %2. The server response was %1
public void SendReport(str email)
{
SysMailer sm = new SysMailer();
;
sm.quickSend("axmail#domain.eu", email, "Test", strfmt("body"));
}
This method is called in Run method. And if I copy this source code into job that works. Additionally in Run method I use,
permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new InteropPermission(InteropKind::ComInterop));
permissionSet.add(new InteropPermission(InteropKind::DllInterop));
CodeAccessPermission::assertMultiple(permissionSet);
Because your email seems to work in a job (client side), your email settings in AX are not the issue.
This is most likely an authentication issue.
Take a look at this article, which describes what your are experiencing.
On the client side, your current windows user is used to authenticate with the SMTP server.
In batch, the emails are sent from the AOS. That means that the account used for authentication, is the account under which the AOS service runs.
Your mail server probably doesn't accept connections from everyone.
Take a look here on how this is achieved.
You'll have to allow the user that runs your AOS service, to authenticate to your mail server.

Email Address Varification Application in ASP.NET

I am getting 555 syntax error in mailfrom
SendData(tcpSocket, string.Format("MAIL From: {0}\r\n", MailFrom));
if (!CheckResponse(tcpSocket, 220))
{
tcpSocket.Close();
return false;
}
is it the problem in my local system because of localhost?
Please help me. I am using this code from below link.
http://www.codeproject.com/Articles/5189/End-to-end-Email-Address-Verification-for-Applicat
Please don't try to implement your own SMTP client, use the one that comes with .NET: System.Net.Mail.SmtpClient.
Many SMTP servers require TLS, for example, which your code does not account for.
Furthermore, for security reasons most mailservers will not reveal if an email address in an RCPT TO line is valid or not. If a system can positively reveal an address exists then it can be used by spam harvesters. Consequently using a dry-run of an SMTP client should only be used to validate an email address (because of the complicated rules regarding valid email addresses). The verification (a separate concept from validation) must be performed manually by requiring the user to respond to an email sent to that address, there is no other way to be sure.

How can I send local Email by using exchange with asp.net?

I have a project that need to sends notification for employees by local email. I use SmtpClient class to send email but didn't work! here is the code:
MailMessage message = new MailMessage();
message.From = new MailAddress("localmail1#company.com");
message.To.Add(new MailAddress("localmail2#company.com"));
message.Subject = "Sending mail";
message.Body = "Check sending email by Exchange from asp.net code <> ";
SmtpClient client = new SmtpClient("ExchangeDNS", 25);
try
{
client.Send(message);
}
catch (Exception exc)
{
Label1.Text = exc.Message.ToString();
}
When I click buttons it give me an SmtpException with message : Failure sending mail.
NB: we use Exchange server.
How can I solve it?
Use the fully qualified name for your server, for example, exchangeDNS.example.com. If that doesn't work, try the IP address. You may want to manually telnet to port 25 on the Exchange server, just to see if it is possible. Also, check whether your server requires an authenticated or encrypted connection. If so, you'll need to supply credentials with the request or change to use SSL and the secure port.
Assuming that you have the server and any required credentials correct (double check these) I would try increasing the timeout on the SMTP client.
Your code is fine. It's the configuration of your Exchange server that is suspect. Some options for your Exchange administrator:
Allow anonymous SMTP connections.
Whitelist the IPs, so that only your web servers can send mail in this manner.
Create an Active Directory account for the web server, and then have your class authenticate using a username and password.
If you post more details about your Exchange configuration, I can help, at the risk of having this question downmodded for "not programming related".

Resources