开发者

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

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 me@abcxyzmail.om without any credentials p开发者_运维百科rovided!

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("admin@webdev.co.nz", "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


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.


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.


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 = "from_address_here@gmail.com";
        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);
    }



}


Add a reply to header

mail.Headers.Add( "Reply-To", "users.email.@hisprovider.com" );

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜