无法从我的asp.net应用程序向不同的域发送电子邮件

时间:2017-12-01 04:54:21

标签: asp.net-mvc smtp

我开发了asp.net应用程序,它将电子邮件发送到我公司的域名。示例:ex@companydomain.com,但我无法将其发送到任何其他域,例如ex@gmail.com或ex1@hotmail.com。我是否需要在smtp服务器或代码中进行更改?请告诉我。我是.net.Below的新手是我使用的代码:

        MailMessage Msg = new MailMessage();
        var MailResult = false;

        var EmailUser = RFSvc.Users.Where(u => u.Email == email).SingleOrDefault();
        if (EmailUser != null)
        {

            Msg.From = new MailAddress("noreply@example.com", "Company name");
            Msg.Subject = "Password Recovery";
            Msg.IsBodyHtml = true;
            Msg.Body = "Your User Name is: " + EmailUser.Username + "<br /> Your password is: " + EmailUser.Password;
            Msg.To.Add(email);

            SmtpClient MailClient = new SmtpClient();

            MailClient.UseDefaultCredentials = false;
            MailClient.Host = "mail.example.com";
            MailClient.Port = 25;
            MailClient.Credentials = new NetworkCredential("username","password");
            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            MailClient.EnableSsl = false;

            try
            {
                MailClient.Send(Msg);

                MailResult = true;
            }
            catch (Exception)
            {
            }
        }

2 个答案:

答案 0 :(得分:0)

嘿,你能提供你使用的端口吗?所以,我可以更多地了解你坚持的地方

Gmail SMTP服务器名称为smtp.gmail.com,使用发送邮件的端口为587,并且还使用NetworkCredential进行基于密码的身份验证。

作为suggested by Trimantra Software Solution

try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("your_email_address@gmail.com");
            mail.To.Add("to_address");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("mail Send");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

希望这对你有所帮助

答案 1 :(得分:0)

我可以将邮件从自定义域发送到gmail.com,这是代码:

var fromAddress = new MailAddress("example@mydomain.com", "From Name");
var toAddress = new MailAddress("example@gmail.com", "To Name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";            

MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.Body = body;


var smtp = new SmtpClient
{
  Host = "smtp.mydomain.com",
  Port = 25,
  EnableSsl = true,
  DeliveryMethod = SmtpDeliveryMethod.Network,
  UseDefaultCredentials = false,
  Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
  Timeout = 20000
};            

smtp.Send(message);

注意:

如果它仍然不适合您,请与您的域名注册商或邮件提供商联系并获取smtp邮件设置,或尝试更改端口465,587,或尝试评论{{1 }}一次检查它是否有效。

参考文献:

1.

2.