System.Net.Mail using Gmail Smtp from address is always mine

1k Views Asked by At

I think that Gmail is rewriting the from address and using the account that is provided in the network credentials for the from.

    MailMessage message = new MailMessage();
    message.From = new MailAddress("[email protected]");
    message.To.Add(new MailAddress("[email protected]"));
    message.Subject = "[Yep] Contact Form";
    message.Body = msg;
    message.IsBodyHtml = false;

    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = false;
    NetworkCredential networkCredentials = new NetworkCredential("[email protected]", "pass");
    client.Credentials = networkCredentials;
    client.EnableSsl = true;
    client.Host = "smtp.gmail.com";
    client.Port = 587;

    try
    {
        client.Send(message);

This is the received email:

From: [email protected] To: [email protected] Date: Sun, 23 Sep 2012 14:44:54 -0700 (PDT) Subject: [Yep] Contact Form Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable

This is a test

I know it use to work but now the from is always mine. Can I get a confirmation if everyone else is having this issue or is it just me?

3

There are 3 best solutions below

0
On

Dim attachmentFile As String = Nothing If FileUpload1.HasFile Then

        Try
            FileUpload1.SaveAs("C:\files\" + FileUpload1.FileName)
            attachmentFile = FileUpload1.PostedFile.FileName
        Catch ex As Exception
            litStatus.Text = "File Upload Failed !! " + ex.Message.ToString()
        End Try


        Try
            Dim mail As New MailMessage()
            Dim SmtpServer As New SmtpClient("smtp.gmail.com")

            mail.From = New MailAddress("[email protected]")




            'you have to provide your gmail address as from address'
            mail.[To].Add(txtTo.Text)
            mail.Subject = txtSubject.Text
            mail.Body = txtBody.Text

            Dim attachment As System.Net.Mail.Attachment
            attachment = New System.Net.Mail.Attachment(attachmentFile)
            mail.Attachments.Add(attachment)

            SmtpServer.Port = 587
            SmtpServer.Credentials = New System.Net.NetworkCredential("gamil-username", "gmail-passowrd")


            'you have to provide you gamil username and password'
            SmtpServer.EnableSsl = True
            SmtpServer.Send(mail)
            litStatus.Text = "Email successfully sent."
        Catch ex As Exception
            litStatus.Text = "Mail Send Failed ! " + ex.Message.ToString()
        End Try

    Else
        litStatus.Text = "Please select a file for uploading"
    End If
3
On

GMail (and many other email providers) will not allow you to alter the FROM header. That would allow email spoofing.

1
On

To achieve this result you will have to go for custom email provider like godaddy or buy business subscription from gmail.

You can also refer Sending Mail from Windows Azure Service, using Godaddy SMTP