次の方法で共有


Sending e-mail from ASP.NET MVC hosted on Azure with a GoDaddy.com domain address

I have a website built with ASP.NET MVC and EntityFramework which is hosted on Microsoft Azure. The domain used for this website was purchased on GoDaddy.com and configured to point to the Azure Websites address. This domain on GoDaddy is purely a domain name with no other features. I wanted to get e-mail working from the website and it turns out that GoDaddy allows for I believe two e-mail accounts to be created for free with the domain name without having to create your own email server or pay for any other service, even through GoDaddy. You can configure those free email accounts through GoDaddy's Workspace Control Center portal as follows:

Once the free email accounts were setup, I was able to configure the EmailService with the below code to send an email via SMTP and the MailMessage/SmtpClient classes as well as the configuration necessary in the web.config file. Here's what you need to do to make e-mail work with your GoDaddy.com domain / email account.

 
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
MailMessage msg = new MailMessage();
#if DEBUG
// Don't send actual emails while debugging, override to send emails to the sender instead.
message.Subject = "[DEBUG] " + message.Subject;
msg.To.Add(msg.From);
#else
// Production, send email -- if multiple recipients, add each recipient to BCC, else the TO line.
var emails = message.Destination.Split(';');
if (emails.Length > 1)
{
foreach (var email in emails)
msg.Bcc.Add(email);
}
else
{
msg.To.Add(message.Destination);
}
#endif
msg.Subject = message.Subject;
msg.Body = message.Body;
msg.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.ServicePoint.MaxIdleTime = 1;
smtp.Timeout = 10000;
await smtp.SendMailAsync(msg);
}
}
}

In my web.config file, I add the configuration for my e-mail SMTP service. This is a great way to not have your configuration hardcoded in code.

 <system.net>
     <mailSettings>
          <smtp
                 from="sender@contoso.com"
                   deliveryMethod="Network">
               <network
                  host="smtpout.secureserver.net"
                 port="3535"
                 userName="sender@contoso.com"
                   password="******"
                   enableSsl="false"
                   defaultCredentials="false" />
          </smtp>
     </mailSettings>
</system.net>
            

Hope this helps!

Comments

  • Anonymous
    August 09, 2016
    Hi Mohammed,Thank you for your post; it has helped me to get email sent after form submission in my MVC App. If you have any guide on how to use the recommended SendGrid service for Azure that would be great. Thanks again!