I need to implement a “email support” section in our application. So email “To” address will be admin#mydomain.com” and from address will be the email address of the end user.(The end users email address may be on the same domain or another domain like user#mydomain.com or user#gmail.com).
In the application I am authenticated the email using admins account details (username and password)
System.Net.NetworkCredential("admin#mydomain.com", adminpassword);
Also I am using host address as “mail.mydomain.com”
Issue is I am getting the following error:
“Mailbox unavailable. The server response was: From address must
match authenticated address” error message.
Is it possible to send an email with the correct sender email address(users from address)
My code sample is
message.To.Add(“admin#mydomain.com”);
message.From = new MailAddress(“test#gmail.com”);
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
var smtp = new SmtpClient("mail.mydomain.com");
smtp.Credentials = new System.Net.NetworkCredential(admin#mydomain.com, adminpassword);
smtp.EnableSsl = false;
object usrtkn = message;
smtp.Send(message);
In general the From address shouldn't be the user themselves, it should be an internal system address. This is mainly because the user isn't actually sending the email, the application is. In the email itself you can specify which user sent it (and what their email address is). You can perhaps even specify the user's email address in the ReplyTo field of the message.
But the message you're getting from the SMTP server pretty much says it all. If the message is "from" that user then the SMTP server refuses it because it's sensitive to authentication and origination of emails. To the SMTP server (to any SMTP server I would imagine) it looks like you're trying to spoof messages.
You cannot do what you are doing, because the SMTP server is not allowing you to "impersonate" the user's email address for sending to the system. And thank goodness this is the case or else people would be spamming/spoofing the heck out of everyone under someone else's name.
Why are you trying to have it appear that the user is sending an email to the application? Why not just have a support section of your application where users can "submit" requests for support to the system and then if you want to send emails out to the users, then your scenario will work, but just in reverse (where the system is the From address and the user is the To address).
I use Golang, I skill put the "From" int othe message
msg := []byte(
"From: root#myserver.com.br\r\n" +
"To: destiny#myserver.com\r\n" +
"cc: destiny2#myservercom\r\n" +
"Subject: Why are you not using Mailtrap yet?\r\n"+
"\r\n"+
"Here’s the space for our great sales pitch\r\n")
auth := smtp.PlainAuth("", root, password, host)
err := smtp.SendMail(address, auth, root, destinationEmail, msg)
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.
I am trying to send message from server email to my gmail
WebMail.SmtpServer = "mail.marekjedrysiak.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = true;
WebMail.UserName = "mail#marekjedrysiak.com";
WebMail.Password = "******";
WebMail.From = "mail#marekjedrysiak.com";
WebMail.Send("marekjedrysiak1#gmail.com", "Message from server",null);
then coming this message :
Please help :(
This isn't an ASP.NET or C# related problem, your domain doesn't have an MX record. You can confirm this by checking http://www.mxtoolbox.com.
I see that your domain is registered with Tucows (Hover). In order to change your MX records:
Log in to your Hover account. If you have multiple domain names, select your marekjedrysiak.com domain.
Click the DNS tab.
Click Add New.
For Record Type, select MX.
Enter the address of your mail server.
Click Save.
If you need more help setting up or debugging your mail service, you should contact Hover's support. They can also advise you on what exactly to enter as the address of your mail server.
Source:
https://help.hover.com/hc/en-us/articles/217282457-How-to-Edit-DNS-records-A-CNAME-MX-TXT-and-SRV-Updated-Aug-2015-
Is it possible to restrict the user to reply to a mail sent via SMTP server?
I have tried using obsolete before the method name but still reply mail could not be blocked
void sendmail()
{
string to = "abc#xyz.com";
string from = "def#abc.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"Test"
SmtpClient client = new SmtpClient(server);
client.UseDefaultCredentials = true;
}
above is the sample code.
As others have said in the comments, it is impractical/impossible (depending on level of access to their computers) to prevent a user replying to an email. However, you can prevent an exchange email account from receiving emails by applying delivery restrictions - telling the account to only receive emails from itself:
On the server running Exchange start "Active Directory Users and Computers"
Find the account.
Double click the account to open its properties (or right click and select "Properties")
On the "Exchange General" tab, click [Delivery Restrictions...].
Under "Message restrictions" check "From authenticated users only". Then select the radio button "Only from" and add the user account. It is important that you add a user account here otherwise Exchange will accept email from any authenticated user.
Click [OK] to close the "Delivery Restrictions" dialog and [OK] again to close the account properties dialog.
Using this method, to any other user it will look like the account does not exist and they will receive delivery failed messages.
This an other methods are detailed here: http://www.cryer.co.uk/brian/msexchange/exch_howto_disable_delivery.htm
I have an asp.net C# webforms project that is hosted on IIS 7.5. Also mail server MailEnable server is used for sending/receiving mail.
In my project user is entering his mail address in the form and I need to send a mail with "from" field set to this user's email address and "to" set to some other user's email address.
Previously everything was working but now something is changed on the server so I've started getting messages:
"The server response was: Sender address is not valid for your login. Check your email program settings." that is 551 error according to http://www.mailenable.com/kb/Content/Article.asp?ID=me020032&SS=
So far I've found that this should be set:
1)"Authenticated senders must use valid sender address" -> disabled
2)"Allow relay for authenticated senders " -> enabled
3)"Address spoofing prevention" is set to "anyone can spoof sender's address"
But this didn't hepl.
What else should I check/change to make it work?
This are some parts of mail-sending code:
setting "from" field:
message.From = new MailAddress(context.From);
setting smtp parameters:
SmtpClient smtpClient = new SmtpClient
{
Host = WebConfigurationManager.AppSettings["SMTP_Server"],
Port = Int32.Parse(WebConfigurationManager.AppSettings["SMTP_Port"]),
Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["SMTP_Login"],
WebConfigurationManager.AppSettings["SMTP_Passw"]),
Timeout = _timeOut,
EnableSsl = Boolean.Parse(WebConfigurationManager.AppSettings["EnableSsl"])
};
I've checked in Plesk and smtp user that I am using for sending mail SMTP_Login/SMTP_Passw exists.
The solution was to change settings as it was mentioned in the question:
"Authenticated senders must use valid sender address" -> disabled
"Allow relay for authenticated senders " -> enabled "Address
spoofing prevention" is set to "anyone can spoof sender's address"
But the last step that I've missed was to restart the MailEnable. Maybe this will be helpfull for someone.
I have a very simple email form that sends out the email using department email account to given recipients. However, the company makes the password expire every 90 days, and there's a web form that I created where the authorized user can go and change the password/email address for the form.
Here's the code:
SmtpClient smtp = new SmtpClient();
smtp.Host = host;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(username, password);
try
{
smtp.Send(iEditMail);
}
catch(SmtpException exception)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, exception.Message + " Make sure the username and password is correct.");
}
Since user is responsible for entering the password, they may accidentally enter the wrong password for the email account or the password may expire. I need to be able to catch this so it can be notified to users who are trying to send email. (Btw, this is only for departmental use). How can I make sure if the credentials are correct before sending the email. Right now it doesn't throw any error to tell the email wasn't sent - we just don't receive it.
First:
If you can, it is better to create a service account and use those credentials. That way there is no need to change the credentials periodically. (#mason's suggestion)
However, if that is not possible:
According to MSDN, the SmtpClient.Send function throws SmtpException if authentication failed. That stinks because that same exception can mean various other things too. But it sounds like you want to verify the user name and password without sending an email. The SMTP protocol certainly allows that, but the SMTPClient class only provides methods for sending email. I don't know of any built-in .NET class that provides what you want.
So I think you need to either
Write your own SMTP code using TCP.
Find a third-party SMTP client that provides more functionality..
Send a dummy email to a dummy address, and look at the StatusCode property to see if there was an authentication problem.
Option number 1 is hard if you care about SSL. If you just use plain text, I think authenticating to SMTP isn't that hard. But more and more mail servers are forbidding that. Option 3 is a hack, but should be easy.
EDIT: Added details about the StatusCode property.
The SMTPClient's Send(MailMessage) method raises a SmtpFailedRecipientsException exception if one or more recipient
addresses were incorrect or unreachable.
Sample Code :
try
{
smtpClient.Send(new MailMessage("test#test.com", "test#test.com", "test", "test"));
return string.Empty;
}
catch (SmtpFailedRecipientException)
{
return string.Empty;
}
catch (Exception ex)
{
return string.Format("SMTP server connection test failed: {0}", ex.InnerException != null ? ex.InnerException.Message : ex.Message);
}
When authenticating, store the username and password in the session. Compare the values of the session with user entries . If bool==true then send the email or else dont.