上传发送邮件后不要ork [英] afte uploading sending mail dont ork

查看:75
本文介绍了上传发送邮件后不要ork的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码scritp从联系我们表单发送邮件到邮件。

但它在我的本地服务器上运行良好,上传后它从不发送它!

  public   string  SendMail( string  toList, string   from  string  ccList, string  subject, string  body)
{

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string .Empty;
try
{
MailAddress fromAddress = new MailAddress( from Mail System);
message.From = fromAddress;
message.To.Add(toList);
if (ccList!= null && ccList!= string .Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true ;
message.Body = body;
smtpClient.Host = smtp.gmail.com; // 我们使用gmail作为我们的smtp客户端
smtpClient.Port = 587 ;
smtpClient.EnableSsl = true ;
smtpClient.UseDefaultCredentials = true ;
smtpClient.Credentials = new System.Net.NetworkCredential( Mail Pass);

smtpClient.Send(message);
msg = 成功< BR>;
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
受保护 void btnSend_Click( object sender,ImageClickEventArgs e)
{
string msg = < center>< h2>< font color = blue>(联系我们SYSTEM)< / h2>< / font>< / center>< br> ; 日期: +
DateTime.Now.ToString()+ < br /> + < br> 名称: + txtName.Text + < br /> +
< br> 电子邮件 + txtEmail.Text + < br /> + < /跨度> <跨度class =code-string>< br> 主题: + drpCat.SelectedItem.Text + < br /> + < h3><峰; br> 邮件:< / H3> +
< h3> + txtMessage.Text + < span class =code-string>
< / h3>;
/ / 创建包含要通过邮件发送的内容的邮件消息对象。
System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage( Sender Mail Recever Mail
drpCat.SelectedItem.Text,msg);

SendMail( Mail Mail null ,drpCat.SelectedItem.Text,msg);


txtEmail.Text = ;
txtName.Text = ;
txtMessage.Text = ;
drpCat.ClearSelection();
lblSent.Visible = true ;
Response.Redirect( Thanx.aspx);
}

解决方案

将UserDefaultCredentials设置为True,告诉SmtpClient忽略凭证中的任何设置属性。这会导致SMTP服务器获取运行代码的ASP.NET帐户的凭据,而不是您提供的凭据。


使用以下功能

< pre lang =c#> public bool SendEmialWithSecure( string ToEmailId, string FromEmailId, string FromName, string SenderEmailId,
string SenderName, string 主题, string MailBody, string SMTPHost, Int32 SMTPPort,
string CredentialEmailId, string CredentialPassword, string strAttachment )
{
System.Net.Mail.MailMessage ResetPassMail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient();
ResetPassMail.To.Add( new MailAddress(ToEmailId));
ResetPassMail.From = new MailAddress(FromEmailId,FromName);
ResetPassMail.Sender = new MailAddress(SenderEmailId,SenderName);
ResetPassMail.Subject =主题;
ResetPassMail.IsBodyHtml = true ;
ResetPassMail.Body = MailBody;
ResetPassMail.Priority = System.Net.Mail.MailPriority.High;
附件attach = new 附件(strAttachment);
ResetPassMail.Attachments.Add(attach);
SmtpServer.Host = SMTPHost;
SmtpServer.Port = SMTPPort;
SmtpServer.Credentials = new NetworkCredential(CredentialEmailId,CredentialPassword);
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
尝试
{
SmtpServer.Send(ResetPassMail);

return true ;
}
catch (例外情况)
{

抛出 ex;
}

}


hi , i have this code scritp that send message from the contact us form to the mail.
but it work good on my local server , after upload the side it never send it !

public string SendMail(string toList, string from, string ccList, string subject, string body)
{

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
    try
    {
        MailAddress fromAddress = new MailAddress(from, "Mail System");
        message.From = fromAddress;
        message.To.Add(toList);
        if (ccList != null && ccList != string.Empty)
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "smtp.gmail.com";   // We use gmail as our smtp client
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("Mail", "Pass");

        smtpClient.Send(message);
        msg = "Successful<BR>";
    }
    catch (Exception ex)
    {
        msg = ex.Message;
    }
    return msg;
}
protected void btnSend_Click(object sender, ImageClickEventArgs e)
{
    string msg = "<center><h2><font color=blue> (Contact Us SYSTEM)</h2></font></center><br>Date: " +
      DateTime.Now.ToString() + "<br />" + "<br>Name: " + txtName.Text + "<br />" +
      "<br>Email " + txtEmail.Text + "<br />" + "<br>Subject: " + drpCat.SelectedItem.Text + "<br />" + "<h3><br>Message:</h3> " +
     "<h3>" + txtMessage.Text + "</h3>";
    //Create Mail Message Object with content that you want to send with mail.
    System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("Sender Mail", "Recever Mail",
  drpCat.SelectedItem.Text, msg);

    SendMail("Mail", "Mail", null, drpCat.SelectedItem.Text, msg);


    txtEmail.Text = "";
    txtName.Text = "";
    txtMessage.Text = "";
    drpCat.ClearSelection();
    lblSent.Visible = true;
    Response.Redirect("Thanx.aspx");
}

解决方案

You set UserDefaultCredentials to True, which tells the SmtpClient to ignore any settings in the Credentials property. This results in the SMTP server getting the credentials of the ASP.NET account your code is running under, NOT the credentials you provided.


use below function

public bool SendEmialWithSecure(string ToEmailId, string FromEmailId, string FromName, string SenderEmailId,
       string SenderName, string Subject, string MailBody, string SMTPHost, Int32 SMTPPort,
       string CredentialEmailId, string CredentialPassword, string strAttachment)
   {
       System.Net.Mail.MailMessage ResetPassMail = new System.Net.Mail.MailMessage();
       SmtpClient SmtpServer = new SmtpClient();
       ResetPassMail.To.Add(new MailAddress(ToEmailId));
       ResetPassMail.From = new MailAddress(FromEmailId, FromName);
       ResetPassMail.Sender = new MailAddress(SenderEmailId, SenderName);
       ResetPassMail.Subject = Subject;
       ResetPassMail.IsBodyHtml = true;
       ResetPassMail.Body = MailBody;
       ResetPassMail.Priority = System.Net.Mail.MailPriority.High;
       Attachment attach = new Attachment(strAttachment);
       ResetPassMail.Attachments.Add(attach);
       SmtpServer.Host = SMTPHost;
       SmtpServer.Port = SMTPPort;
       SmtpServer.Credentials = new NetworkCredential(CredentialEmailId, CredentialPassword);
       SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
       try
       {
           SmtpServer.Send(ResetPassMail);

           return true;
       }
       catch (Exception ex)
       {

           throw ex;
       }

   }


这篇关于上传发送邮件后不要ork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆