开发者

email from someaddress@company.com in asp.net using gmail SMTP authentication

开发者 https://www.devze.com 2023-03-18 05:21 出处:网络
Is it possible to use gmail\'s SMTP server to send emails in asp.net C# but display from address as \'someone@companyname.com\'?

Is it possible to use gmail's SMTP server to send emails in asp.net C# but display from address as 'someone@companyname.com'?

I am using code from http://www.programmerfish.com/send-email-using-gmail-in-aspnet-c/

Any example or URL will be beneficial.开发者_Go百科

Thanks, Ali


Have you tried modifing the follow line...

msg.From = new MailAddress(gMailAccount);

to msg.From = new MailAddress("someone@companyname.com");

As your passing in the "real" gmail account to this line...

NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password)

you should be able to stick anything in the msg.From line


Add mail settings to Web.conf in the system.net section, use can set the default from address in smtp section.

<mailSettings>
  <smtp from="someone@companyname.com">
    <network enableSsl="false" host="smtp.gmail.com" 
             password="secretone" port="25" userName="user@gmail.com" />
  </smtp>
</mailSettings>

then in your C# code you have a option to vary it by passing a from parameter to MailMessage.

var smtp = new SmtpClient();
smtp.Send(new MailMessage("someone@companyname.com", "to@domain.com")
{
    IsBodyHtml = true,
    Subject = "Some subject",
    Body = "add html code here"
});

that will do the job


The above suggestions are correct, however the biggest problem is that Google will send mail from their IP address and the from server's registered domain name ip address will be different. Your emails will have a higher potential to be flagged as spam. You may also want to check the TOS for gmail as it may violate the TOS to use it in that fashion.

I'm a developer and not a system admin, however I recall hearing something similar to the above in the past. If you end up with complaints that the messages are going to spam that may be the cause.

0

精彩评论

暂无评论...
验证码 换一张
取 消