Change Sender Address When Sending Mail Through Gmail in C#

change sender address when sending mail through gmail in c#

Gmail doesn't allow you to change the FROM to something different than your gmail account.

It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.

Setting a different From address for mail sent via Gmail using C#

I just found out the answer by testing with another SMTP server. This is actually caused by GMail not allowing any other from address. This works fine with other SMTP servers.

Thanks to leppie, Mikael Svenson and smirkingman for their suggestions.

Sending email from secondary address using gmail SMTP

Looks like what I had to do was just wait...
Now it works fine without changing any code.

Can I change the sender name for sending email in MVC .net Application?

Try below.

mail.From = new MailAddress("myemail@gmail.com","Your display name");

OR

using(MailMessage message = new MailMesage(
new MailAddress("myemail@gmail.com", "Your display Name"),
));

C# sending email via Gmail account

If your Gmail account has 2-Step Verification enabled you will have to create an App-Specific Password to authenticate with instead.

Note also that SmtpClient is IDisposable - you should be putting it in a using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)) { ... } block so that the SMTP connection RSETs, QUITs and closes correctly.

== edit ==

Also, it appears you have the from and recipients parameters switched around on smtpClient.Send.

string body = "<head>" +
"Here comes some logo" +
"</head>" +
"<body>" +
"<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
"<a>Dear User, </a>" + Environment.NewLine +
"<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
"<a>This is the last step towards using our app.</a>" + Environment.NewLine +
"<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
"<a>[Callback url]</a>" +
"</body>";
try
{
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential()
{
UserName = Config.Username,
Password = Config.Password,
};
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;

//Oops: from/recipients switched around here...
//smtpClient.Send("targetemail@targetdomain.xyz", "myemail@gmail.com", "Account verification", body);
smtpClient.Send("myemail@gmail.com", "targetemail@targetdomain.xyz", "Account verification", body);
}
}
catch (Exception e)
{
Console.Error.WriteLine("{0}: {1}", e.ToString(), e.Message);
}

How to use any email address in the FROM field while sending email through Gmail SMTP?

Having run your code snippet I get:

Return-Path: <my user>
Received: from Psi ([80.92.234.64])
by mx.google.com with ESMTPS id f1sm20531634wiy.2.2012.10.08.10.07.49
(version=TLSv1/SSLv3 cipher=OTHER);
Mon, 08 Oct 2012 10:07:49 -0700 (PDT)
Message-ID: <50730865.2152b40a.13ea.28ec@mx.google.com>
Sender: Roman R. <my user>
MIME-Version: 1.0
From: any email
To: my email
Date: Mon, 08 Oct 2012 10:07:49 -0700 (PDT)
Subject: Subject
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Body

Sender is the email address used to authenticate with Google Mail. From is the "from" provided in code. The receiving application might be confusing the two, and the rest looks just as expected. Some mail clients present the From+Sender (when they are different) as "sent by Sender on behalf of From".

You might be concerned with the fact that Google Mail still reveal the account from which the email is sent, through Sender field, but this is how it works. You do send from this account.

And, another possible reason is the From mail address itself. If you added it to your Google Mail account as one of your own addresses (and confirmed via test email with a link), then Google Mail will allow putting it onto From field. Otherwise it might drop it and replace it with the Sender.

Need to change FromEmail String when sending Email through SMTPClient

Create a MailAddress with the real name and email address, then supply that to the MailMessage.

From MSDN (From property for MailMessage).

  MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);


Related Topics



Leave a reply



Submit