How to send mail in ASP.Net from google, yahoo or other mail domains?

3.6k Views Asked by At

I have a "Contact Us" page where in users will give in their email id and a query and on submitting the form, web admin would receive that email.

If I configure their email id to "from" MailAddress and send the mail, it will fail to do so if the ID is from popular mail domains like gmail or hotmail but would work with other unpopular or non existent domains like [email protected] without any credentials provided!

It worked with gmail after I configured SMTP and network credentials properly. The aim is to let the admin of my website who receives the email be able to hit the reply button in his mail client and see the "to" field populated with the "from" field filled in "contact us" page. Is there any proper way to do this or a tip or trick to accomplish it.

Heres my code

    MailMessage emailMessage = new MailMessage();
    MailAddress emailTo = new MailAddress("[email protected]", "Web Dev");
    MailAddress emailFrom = new MailAddress(tbEmail.Text);
    SmtpClient localhost = new SmtpClient("localhost");

    emailMessage.To.Add(emailTo);
    emailMessage.From = emailFrom;
    emailMessage.Subject = "Enquiry / Feedback";
    emailMessage.Body = "Name: " + tbName.Text +
            "\nAddress: " + tbEmail.Text +
            "\nComments: " + tbComments.Text;//emails body

    localhost.Send(emailMessage);

Thanks

Sid

4

There are 4 best solutions below

2
On BEST ANSWER

Not sure why you've got problems here -- we've got a few systems that do just this without any issues. But mail is a finnicky and hinky beast; I would bet on a configuration setting on the server messing things up -- how much control do you have there?

In any case, the more proper way to do this is use the EmailMessage.ReplyTo (2.0/3.5) or EmailMessage.ReplyToList (4.0) property to send the messages. This will probably bypass any configuration on the server that is causing this problem.

2
On

This is because you are using your localhost to send the email - you need an email server. If you actually have a GMail (or whatever) account - then use their server with the correct credentials.

0
On
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;


public class YourClass
{
    private void SendMailFromGmail(string vFrom, string vTo, string vGmailID, string vGmailPass, string vMailText, string vSMPTDNS, string vSubject)
    {
        MailMessage MyMailMessage = new MailMessage();
        SmtpClient SMTPServer = new SmtpClient(vSMPTDNS);
        var _with1 = SMTPServer;
        //Start by creating a mail message object

        //From requires an instance of the MailAddress type

        MyMailMessage.From = new MailAddress(vFrom);

        //To is a collection of MailAddress types
        MyMailMessage.To.Add(vTo);    
        MyMailMessage.Subject = vSubject;
        MyMailMessage.Body = vMailText;
        //Create the SMTPClient object and specify the SMTP GMail server
        _with1.Port = 587;

        _with1.Credentials = new System.Net.NetworkCredential(vGmailID, vGmailPass);
        _with1.EnableSsl = true;

        try {
            _with1.Send(MyMailMessage);
            string lNewVariable5 = "Email Sent";
        //MessageBox.Show(lNewVariable5)
        } catch (SmtpException ex) {
            throw ex;
        }
    }

    public void Main()
    {
        string vFrom = "[email protected]";
        string vTo = "to_address_here@domain_name_here";
        string vGmailID = "account uid";
        string vGmailPass = " account pwd";
        string vMailText = "This is the test text for Gmail email";
        string vSMPTDNS = "smtp.gmail.com";
        string vSubject = "GMail Test";

        SendMailFromGmail(vFrom, vTo, vGmailID, vGmailPass, vMailText, vSMPTDNS, vSubject);
    }



}
1
On

Add a reply to header

mail.Headers.Add( "Reply-To", "[email protected]" );

This will make the email client to populate this address instead of from address. Is this what you are looking for. The above code is untested.