MailMessage smtpClient在发布后无法正常工作

时间:2015-04-15 10:43:53

标签: c# asp.net smtp mailmessage

以下代码在使用 localhost 进行调试时工作正常,但在我发布到Web服务器后却没有。

protected void SendMail()
    {

        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress("xxxxxx@gmail.com");
        mailMessage.To.Add("xxxxxx@gmail.com");
        mailMessage.Subject = ddlSubject.Text;

        mailMessage.Body = "<b>Sender Title: </b> " + ddlTitle.SelectedItem.Text + "<br/>"
            + "<b>Sender First Name: </b> " + txtFName.Text + "<br/>"
            + "<b>Sender Last Name: </b> " + txtLName.Text + "<br/>"
            + "<b>Sender Email: </b> " + txtEmail.Text + "<br/>"
            + "<b>Sender Phone: </b> " + txtPhone.Text + "<br/>"
            + "<b>Sender Address: </b> " + txtAddress.Text + "<br/>"
            + "<b>Sender Suburb: </b> " + ddlSuburbs.SelectedItem.Text + "<br/>"
            + "<b>Sender Message: </b> " + contactMessage.InnerText + "<br/>";
        mailMessage.IsBodyHtml = true;

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
        smtpClient.Credentials = new System.Net.NetworkCredential("xxxxxxx@gmail.com", "xxxxxx");
        smtpClient.EnableSsl = true;
        smtpClient.Send(mailMessage);

        lblConfirmation.Text = "Thank you " + ddlTitle.SelectedItem.Text + " " + txtFName.Text.ToUpper() + " " + txtLName.Text.ToUpper() + " for contacting us. We will try to get back to you ASAP";
    }

注意网站正在使用SSL。

1 个答案:

答案 0 :(得分:0)

确保port = 25。

  try 
  { 
           MailMessage msg = new MailMessage ();
           MailAddress fromAdd = new MailAddress("fromemail@email.com");
           msg.[To].Add("toemail@email.com");
           msg.Subject = "Choose Session Members";
           msg.From = fromAdd;
           msg .IsBodyHtml = true;
           msg.Priority = MailPriority.Normal;
           msg .BodyEncoding = Encoding.Default;
           msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
           msg.Body = msg.Body + "</table></center>";
           SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
           smtpClient.EnableSsl = true;
           smtpClient.UseDefaultCredentials = false;
           smtpClient.Credentials = new System.Net.NetworkCredential("yourname@yourserver.com", "password");
           smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
           smtpClient.Send(msg);
           smtpClient.Dispose();
        }
    catch (exception ex){
        Label1.Text  = ex.ToString();
    }