Joshua
Joshua

Reputation: 2295

receive email through asp.net application

I want to receive email to my email address through my asp.net application. Its like someone who send an inquiry through a form. I have used the following code for this and seems like its doing nothing. I remember that I did one of my websites and cant remember how I did it. Please find the code below.

Thanks,

eMessage.To = "[email protected]"
        eMessage.From = txtEmail.Text
        eMessage.Subject = "Web Submission"
        eMessage.Body = "Web submission received from " & txtName.Text & ". Phone no: " & txtPhone.Text & "."
        eMessage.Priority = MailPriority.High
        SmtpMail.Send(eMessage)

How can I make this working?

Upvotes: 3

Views: 9087

Answers (2)

Sam
Sam

Reputation: 5677

I set up a google account for my business like [email protected]. And I use that as a relay. You have to set your google account to "Allow less secure apps".

Here is my code to let a potential client fill out a contact us from and send the info to me (Even works when I publish to Azure:)):

private void SendEmailToMyCompany(ContactInfo contactInfo)
    {
        string message = contactInfo.Message.Replace("\n", "<br />");

        MailAddress from = new MailAddress(contactInfo.Email);
        MailAddress to = new MailAddress("[email protected]");
        MailMessage mailMessage = new MailMessage(from, to);

        StringBuilder body = new StringBuilder();
        body.AppendFormat($"<b>First Name:</b> {contactInfo.FirstName}");
        body.Append("<br />");
        body.AppendFormat($"<b>Last Name:</b> {contactInfo.LastName}");
        body.Append("<br />");
        body.AppendFormat($"<b>Phone:</b> {contactInfo.Phone}");
        body.Append("<br />");
        body.AppendFormat($"<b>Email:</b> {contactInfo.Email}");
        body.Append("<br />");
        body.AppendFormat($"<b>Message:</b><br /><br /> {message}");

        mailMessage.Body = body.ToString();
        mailMessage.Subject = "MyCompany Customer Contact";
        mailMessage.IsBodyHtml = true;

        string smtpHost = _config["EmailSettings:SmtpHost"];
        string port = _config["EmailSettings:Port"];
        string userName = _config["EmailSettings:UserName"];
        string password = _config["EmailSettings:Password"];

        SmtpClient client = new SmtpClient(smtpHost)
        {
            Port = int.Parse(port),
            Credentials = new NetworkCredential(userName, password),
            EnableSsl = true
        };

        client.Send(mailMessage);
    }

And then here is my email settings from app.config:

"EmailSettings": {
"SmtpHost": "smtp.gmail.com",
"Port": 587,
"UserName": "[email protected]",
"Password": "**********"

}

Upvotes: 0

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

Reputation: 14595

Your sample code shows how to use SMTP to send emails, but you won't be able to retrieve emails from a remote server using this protocol.

The two most common protocols used to retrieve emails are POP3 and IMAP4, and unfortunately the .NET framework doesn't provide an implementation of them like it is done with SMTP.

One option for email retrieval is to use use the open source POP3 client OpenPop.NET, which is discussed in this SO question: retrieve email using c#?.

Upvotes: 10

Related Questions