使用.net中的SmtpClient发送邮件

时间:2010-03-18 14:40:34

标签: c# .net asp.net smtpclient

我无法使用smtp客户端发送邮件。 这是代码:

SmtpClient client=new SmtpClient("Host");
client.Credentials=new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.from="sender@gmail.com";
mailMessage.To.Add("recipient@gmail.com");
mailMessage.body="body";
mailMessage.subject="subject";
client.Send(mailMessage);

问题是当我在ASP.NET应用程序中使用此代码时,我没有收到任何邮件。在asp.net中,我将从邮件地址更改为NetworkCredential中给出的用户名,我收到邮件。

但在C#windows应用程序中,即使发件人的电子邮件地址无效,我也可以收到电子邮件。

5 个答案:

答案 0 :(得分:15)

我发现在设置'UseDefaultCredentials = false'UseDefaultCredentials = false之前设置SmtpClient Credentials属性会导致忽略凭据。

失败:

SmtpClient smtp = new SmtpClient;
smtp.Credentials = new NetworkCredential("user","pass");
smtp.UseDefaultCredentials = false;

使用:

SmtpClient smtp = new SmtpClient;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("user","pass");

去图。

答案 1 :(得分:1)

这表示您的邮件服务器不允许邮件中继。您的邮件服务器只允许您从经过身份验证的电子邮件ID发送邮件作为用户名。通常这样做是为了防止邮件作为不同于身份验证的身份发送。

答案 2 :(得分:0)

试试这个:

MailMessage mail = new MailMessage("emailfrom","emailto");

mail.From = new MailAddress("emailfrom");
mail.Subject = txtsbjct.Text;
string Body = txtmsg.Text;
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("youremail", "yourpassword");

smtp.EnableSsl = true;
smtp.Send(mail);
txtemail.Text = "";
txtmsg.Text = "";
txtsbjct.Text = "";
Label1.Text = "your email has been send";
mail = null;
smtp = null;

答案 3 :(得分:0)

简短答案:请勿在内部SMTP客户端类中使用.net,而应使用MailKit

长答案:

  1. 它被标记为obsolete in MS docs
  

[System.Obsolete(“ SmtpClient及其类型的网络设计不良,我们强烈建议您改为使用https://github.com/jstedfast/MailKithttps://github.com/jstedfast/MimeKit”]]   公共类SmtpClient:IDisposable

  1. 使用oss库MailKit
  2. 详细说明:因为您将尽快而不是稍后面对其缺陷。 Read here或在互联网上阅读。
  3. 以下是sending条普通消息和multipart条消息的示例。

我故意不在此处从文档中复制示例,因为文档可能会随着时间而变化,因此最好阅读。

答案 4 :(得分:-4)

 <?php
$result = $mysqli->query("SELECT id FROM table ");
$total_results = $result->num_rows;
$result->close();

    /* Setup vars for query. */

    $targetpage = "moving-articles";
    $limit = 15; 
    $adjacents = 5;
    $lastpage = ceil($total_results/$limit);                                
    $page = $_GET['page'];
    if( $page) 
        $start = ($page - 1) * $limit;  
    else
        $start = ($lastpage - 1) * $limit;; 

    /* Get data. */
    $sql = $mysqli->query("(SELECT * FROM table ORDER BY datestamp LIMIT $start, $limit) ORDER BY datestamp DESC");
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lpm1 = $lastpage - 1;                          

    if ($page == '') $page = $lastpage; //if no page var is given, default to last page (as first page)
    /* Draw the pagination object.  */

 $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "
        <div class='pagination'>";
        /* Next Button */
        if ($page < $lastpage) 
            $pagination.= "<a href='$targetpage/page/$next/'>« next</a>";
        else
            $pagination.= "<span class='disabled'>« next</span>";   

        if ($lastpage < $adjacents * 4) //not enough pages to bother breaking it up
        {   
        /* Backwards Pagination */
        for ($counter = $lastpage; $counter > 0; $counter--) {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";
        }
        }

        else if($lastpage > $adjacents * 4) //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page > $lastpage - $adjacents * 2)      
            {

                for ($counter = $lastpage ; $counter > $adjacents * 2; $counter--)
                {
                    if ($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";                  
                }

                $pagination.= "...";
                $pagination.= "<a href='$targetpage/page/2/'>2</a>";
                $pagination.= "<a href='$targetpage/page/1/'>1</a>";

            }
        //in middle; hide some front and some back
            else if($page < $lastpage - $adjacents * 2 && $page > $adjacents * 2)
            {

                $pagination.= "<a href='$targetpage/page/$lastpage/'>$lastpage</a>";    
                $pagination.= "<a href='$targetpage/page/$lpm1/'>$lpm1</a>";
                $pagination.= "...";
                for ($counter = $lpm1 - $adjacents; $counter - $adjacents *2 > 2; $counter--)
                {
                    if ($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href='$targetpage/page/2/'>2</a>";
                $pagination.= "<a href='$targetpage/page/1/'>1</a>";

            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href='$targetpage/page/$lastpage/'>$lastpage</a>";    
                $pagination.= "<a href='$targetpage/page/$lpm1/'>$lpm1</a>";
                $pagination.= "...";
                for ($counter = $lpm1 - $adjacents * 2; $counter >= 1; $counter--)
                {
                    if ($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";                  
                }
            }

        }


        /* Previous Button */
        if ($page > $counter - 1) 
            $pagination.= "<a href='$targetpage/page/$prev/'>previous »</a>";
        else
            $pagination.= "<span class='disabled'>previous »</span>";

        $pagination.= "</div>";     
    }

?>