Sending Email via IIS SMTP to external address - asp.net

When using the following code to send an email message to an external e-mail address via IIS6 SMTP I am receiving a message stating that the message has been sent, but it never arrives at the destination. I'm using the System.Net.Mail namespace and the following code:
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
foreach (string strTo in to.Split(';'))
{
if (strTo.Replace(";", "") != string.Empty)
msgMailSummary.To.Add(new MailAddress(strTo.Replace(";", "")));
}
msg.Subject = subject;
msg.Body = body;
SmtpClient sm = new SmtpClient();
sm.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sm.Credentials = new NetworkCredential(tbUsername.Text, tbPassword.Text);
sm.Host = host;
sm.Port = port;
sm.Send(msg);
I don't have a SmartHost setup in IIS6, is there any obvious or any hints,tips that I can check out to get this working?

Umm, you seem to be missing one key line...
msg.To = new MailAddress(to);

I am not sure if I remember right, but I once had a problem where I couldn't send an email because my From address was not what my hosting allowed. Basically I ended up being able to only set ReplyTo and leaving From undefined (the smtp server will define it by itself). Try it, it might work.

Turned out it was the setup on the server wasn't configured correctly.

Related

Force System.Net.Mail.SmtpClient to encode the Subject header using Base64

We are using .NET Core 3.1's default SMTP client to send an email, like this:
private async Task SendMail(string from, string to, string subject, string body)
{
var message = new MailMessage();
message.From = new MailAddress(from);
var toAddress = new MailAddress(to);
message.To.Add(toAddress);
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = false;
using var smtp = new SmtpClient();
smtp.Host = SMTP_HOST;
smtp.Port = SMTP_PORT;
smtp.EnableSsl = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.DeliveryFormat = SmtpDeliveryFormat.International;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(SMTP_USER, SMTP_PASSWORD);
await smtp.SendMailAsync(message);
}
Then we call the method like this:
await this.SendMail(
from: "noreply#ourdomain.com",
to: "recipient#example.com",
subject: "Caractères accentués",
body: "dummy"
);
When using a network analyzer to check how the Subject header is encoded, we are observing the following:
When the client connects to a MTA that supports SMTPUTF8, the Subject header is encoded using UTF8: Subject: Caractères accentués.
When the client connects to a MTA that does not support SMTPUTF8, the Subject header is encoded in Base64 then in US-ASCII: Subject: =?utf-8?B?Q2FyYWN0w6hyZXMgYWNjZW50dcOpcw==?=.
If we switch from SmtpDeliveryFormat.International to SmtpDeliveryFormat.SevenBits, the Subject header is always encoded in Base64 then in US-ASCII (or maybe quoted-url then US-ASCII).
This is all standard and excpected behaviour.
We are using a third party email service as a SMTP relay, which supports SMTPUTF8 itself. But their service has a bug and fails to detect the lack of SMTPUTF8 support on recipient's side, resulting in email Subjects being improperly displayed in our clients mailboxes when they contain non-ASCII characters. They send the Subject with the same encoding we used with their MTA, which in our case is UTF8, because we need SmtpDeliveryFormat.International (for compatibility with non-ASCII email addresses).
The issue disappears when we encode the Subject header using Base64, so we would like to do that as a workaround until our provider fixes the issue on their side. Using smtp.DeliveryFormat = SmtpDeliveryFormat.SevenBits achieves this, but it also prevents us from using non-ASCII characters in email addresses, which is an even bigger problem than the Subject encoding issue. So we can't do that.
Is there a way to force the .NET client to use Base64 encoding for the Subject header while also using SmtpDeliveryFormat.International, even when the SMTP relay supports SMTPUTF8? I tried to do this:
message.Subject = $"=?utf-8?B?{Convert.ToBase64String(Encoding.UTF8.GetBytes(subject))}?=";
But the subject header is not passed through, it is decoded by the SmtpClient then UTF8-encoded as Caractères accentués, so it doesn't change anything.

how to add message in contact us page to inbox?

In every website there is "contact us"
which contains "name ,message,mobno,mailid are may be mandatory and submit(button) "
how to do this in html?
When I click submit buton,is it possible to get message added to gmail inbox automatically????
You can do this. Here is the example
<form method="post" action="mailto:email#serviceprovide.com" >
<input type="submit" value="Send Email" />
</form>
There are many way in which you can send email, you can do it in C#,javascript,any of the email form generator.
You can decide which one to use based on what you actually need and is best satisfied by which method.
You need to Code for send Mail to your account in the submit button event like below
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;// only for gmail account
smtp.Credentials=new System.Net.NetworkCredential("AdminEmail#admin.com","AdminPassword");
smtp.EnableSsl = true;
smtp.Send(Msg);
Also you need a Gmail account (user name and password) to send mail
If you are using any other smtp server then smtp.Port = 25; the port number will change
refer this link

asp.net The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required [duplicate]

This question already has answers here:
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
(21 answers)
Closed 7 years ago.
hi i know this question is a bit redundant but i am experiencing it differently here is my code on sending email after creation of account on asp.net
Dim newreg As MembershipUser = Membership.GetUser(context.Request("username"))
Dim newid As Guid
If newreg.ProviderUserKey IsNot Nothing Then
newid = DirectCast(newreg.ProviderUserKey, Guid)
End If
Dim body As String = String.Empty
Dim reader As StreamReader = New StreamReader(context.Server.MapPath("~/Account/email.htm"))
body = reader.ReadToEnd
body = body.Replace("{UserName}", context.Request("username").ToString)
body = body.Replace("{Url}", "http://wwww.123.com/Account/activate.aspx?id=" & context.Request("username").ToString & "&usertype=" & givetype(context.Request("username")) & "&actid=" & newid.ToString)
Dim mailMessage As MailMessage = New MailMessage
mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))
mailMessage.Subject = "Account Activation"
mailMessage.Body = body
mailMessage.IsBodyHtml = True
mailMessage.To.Add(New MailAddress(context.Request("email").ToString))
Dim smtp As SmtpClient = New SmtpClient
smtp.Host = ConfigurationManager.AppSettings("Host")
smtp.EnableSsl = True
Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential
NetworkCred.UserName = ConfigurationManager.AppSettings("UserName")
NetworkCred.Password = ConfigurationManager.AppSettings("Password")
smtp.UseDefaultCredentials = False
smtp.Credentials = NetworkCred
smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("Port"))
smtp.Send(mailMessage)
the odd thing is when i run the website on localhost gmail allows it but when i run the website on a vps it rejects and gives this error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
any help will do tnx
got it working the problem was gmail is blocking the request to send the email because of the 2 step verification i created a password for the app and now gmail is not blocking the request to send e-mails hope this helps if somebody will encounter the same issue with gmail

receive email through asp.net application

I want to receive email to my email address through my asp.net application. Its like someone who send an inquiry through a form. I have used the following code for this and seems like its doing nothing. I remember that I did one of my websites and cant remember how I did it. Please find the code below.
Thanks,
eMessage.To = "info#boilernetworkservices.co.uk"
eMessage.From = txtEmail.Text
eMessage.Subject = "Web Submission"
eMessage.Body = "Web submission received from " & txtName.Text & ". Phone no: " & txtPhone.Text & "."
eMessage.Priority = MailPriority.High
SmtpMail.Send(eMessage)
How can I make this working?
Your sample code shows how to use SMTP to send emails, but you won't be able to retrieve emails from a remote server using this protocol.
The two most common protocols used to retrieve emails are POP3 and IMAP4, and unfortunately the .NET framework doesn't provide an implementation of them like it is done with SMTP.
One option for email retrieval is to use use the open source POP3 client OpenPop.NET, which is discussed in this SO question: retrieve email using c#?.
I set up a google account for my business like myCompanyName#gmail.com.
And I use that as a relay.
You have to set your google account to "Allow less secure apps".
Here is my code to let a potential client fill out a contact us from and send the info to me (Even works when I publish to Azure:)):
private void SendEmailToMyCompany(ContactInfo contactInfo)
{
string message = contactInfo.Message.Replace("\n", "<br />");
MailAddress from = new MailAddress(contactInfo.Email);
MailAddress to = new MailAddress("myhotmailaccount#hotmail.com");
MailMessage mailMessage = new MailMessage(from, to);
StringBuilder body = new StringBuilder();
body.AppendFormat($"<b>First Name:</b> {contactInfo.FirstName}");
body.Append("<br />");
body.AppendFormat($"<b>Last Name:</b> {contactInfo.LastName}");
body.Append("<br />");
body.AppendFormat($"<b>Phone:</b> {contactInfo.Phone}");
body.Append("<br />");
body.AppendFormat($"<b>Email:</b> {contactInfo.Email}");
body.Append("<br />");
body.AppendFormat($"<b>Message:</b><br /><br /> {message}");
mailMessage.Body = body.ToString();
mailMessage.Subject = "MyCompany Customer Contact";
mailMessage.IsBodyHtml = true;
string smtpHost = _config["EmailSettings:SmtpHost"];
string port = _config["EmailSettings:Port"];
string userName = _config["EmailSettings:UserName"];
string password = _config["EmailSettings:Password"];
SmtpClient client = new SmtpClient(smtpHost)
{
Port = int.Parse(port),
Credentials = new NetworkCredential(userName, password),
EnableSsl = true
};
client.Send(mailMessage);
}
And then here is my email settings from app.config:
"EmailSettings": {
"SmtpHost": "smtp.gmail.com",
"Port": 587,
"UserName": "myCompanyNameGmailAccount#gmail.com",
"Password": "**********"
}

E-Mails sent from online form are coming in one single mail

I'm using an online form at one of my web sites. Every mail sent from this form is coming in one mail even if the senders IP is different.
But I want every single mail to be unique even if the content is same. What I need to do to the mails or which header I need to modify?
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("no-reply#toprakbasim.com", "NoReply");
MailAddress toAddress = new MailAddress("info#toprakbasim.com", "Info");
MailAddress toSender = new MailAddress(tEMail.Text, tNameSurname.Text);
message.From = fromAddress;
message.Bcc.Add(toAddress);
message.ReplyTo = toSender;
message.Subject = tNameSurname.Text + " : contact";
message.IsBodyHtml = true;
message.Body = "some html here";
smtpClient.Send(message);
Gmail will group emails with the same subject line together. Put some text in the subject line to make it unique, like MessageID, time, whatever.
If you are saying the content in the body contains more than one response, then the issue is how you are collecting the text that then gets assigned to message.Body. If the text is in a variable before assigning to message.Body, make sure that you are not re-using the variable and that it gets re-instantiated each time.
What are you using for a mail reader program? Because it sounds like that program is rolling your emails up for you. (Outlook 2010 does this, by default).
Try reading your email with a different email reader (like Outlook Express, or tbird)

Resources