How to set From Address to any email other gmail in ( Sending Email in .NET Through Gmail )?

前端 未结 4 800
谎友^
谎友^ 2021-01-22 21:05

In this post Sending Email in .NET Through Gmail we have a code to send email through gmail, in the send mail we find from Field contain gmail account that I used
I use the

4条回答
  •  孤独总比滥情好
    2021-01-22 21:36

    You can import an email id in your gmail account using Mail Settings >> Accounts and Import options and that can be used for sending the mails, however if you are want to use some random email id everytime to send the mails it is not possible. Gmail will treat that as a spoofing/spam and it will reset the mail address to your original mail id before sending the mail.

    using System.Net;
    using System.Net.Mail;
    
    public void email_send()
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("from@gmail.com");
        mail.To.Add("to@gmail.com");
        mail.Subject = "Your Subject";
        mail.Body = "Body Content goes here";
    
        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment("c:/file.txt");
        mail.Attachments.Add(attachment);
    
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("from@gmail.com", "mailpassword");
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    
    }
    

    There are many other mail services from which you can achieve the same but not through the gmail. Checkout the blog Send email in .NET through Gmail for sending mail using different properties.

提交回复
热议问题