簡體   English   中英

如何通過ASP.NET Web API發送電子郵件

[英]how to send email through asp.net web api

我正在制作Windows Phone應用程序,希望在其中提供忘記密碼功能。 當用戶按下“忘記密碼”按鈕時,該應用程序將向用戶存儲的電子郵件ID發送一封電子郵件。 由於Windows Phone中沒有可用的smtp類,因此我想制作一個asp.net Web API,它將從應用程序接收電子郵件ID(和密碼),並將電子郵件發送給該ID。 我是Web服務的新手,對此有0的知識,請指導我如何完成此任務,如果任何人都可以提供代碼,則必須有一些可用的Web服務。

這是發送可以使用的電子郵件的功能的示例。 另外,下面有幾個鏈接可以指導您在ASP.NET中創建Web服務

實際上,您無需為此創建Web服務(盡管建議這樣做)。 您可以創建一個快速且骯臟的Web表單,在其中傳遞諸如example.com/sendemail.aspx?account=jack.smith&id=223232343這樣的參數

private static void SendEmail(string from, string from_name, string to, string cc, string bcc, string subject, string body, bool isHtml)
{
    SmtpClient mailClient = new SmtpClient(Config.SmptSettings.Server);
    mailClient.Credentials = new NetworkCredential(Config.SmptSettings.UserName, Config.SmptSettings.Password);
    mailClient.Port = Config.SmptSettings.Port;

    MailMessage message = new MailMessage();
    if (!string.IsNullOrEmpty(from_name))
    {
        message.From = new MailAddress(from, from_name);
    }
    else
    {
        message.From = new MailAddress(Formatter.UnFormatSqlInput(from));
    }

    message.To.Add(new MailAddress(to));

    if (!string.IsNullOrEmpty(bcc, cc))
    {
        message.CC.Add(cc);
        message.Bcc.Add(bcc);
    }

    message.Subject = subject;
    message.Body = body;
    message.IsBodyHtml = isHtml;

    mailClient.EnableSsl = Config.SmptSettings.SSL;
    mailClient.Send(message); 
}
 ## Call this function in your WebApi controller ##

    private void sendEmailViaWebApi()
    {
                string subject = "Email Subject";
                string body = "Email body";
                string FromMail = "shahid@reckonbits.com.pk";
            string emailTo = "reciever@reckonbits.com.pk";
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("mail.reckonbits.com.pk");

                mail.From = new MailAddress(FromMail);
                mail.To.Add(emailTo);
                mail.Subject = subject;
                mail.Body = body;

                SmtpServer.Port = 25; 
                SmtpServer.Credentials = new System.Net.NetworkCredential("shahid@reckonbits.com.pk", "your password");
                SmtpServer.EnableSsl = false;
                SmtpServer.Send(mail);
    }

對於使用.NET Core的用戶,請注意,當前不支持發送電子郵件。

通過GitHub看到有關.NET Core的以下問題: https : //github.com/dotnet/corefx/issues/1006

已經實現了一個替代解決方案-MailKit: https//github.com/jstedfast/MailKit

string random;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string s1 = string.Empty;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select EmailId from tblEmail where EmailId='" + txtemail.Text + "'", con);
    SqlDataReader dr =cmd.ExecuteReader();

    if (dr.HasRows)
    {
        while (dr.Read())
        {
            s1 = dr.GetString(0);
        }
    }
    dr.Close();

    if (s1.Equals(txtemail.Text))
    {
        Session["Email"] = txtemail.Text;

        try
        {
            Random rndm = new Random();
            random = rndm.Next(99999).ToString();
            MailMessage message = new MailMessage();
            message.From = new MailAddress("yourid@gmail.com");
            message.To.Add(new MailAddress(txtemail.Text));
            message.Subject = " Hello... This is Your Serial No to change your password:";
            message.Body = random.ToString();
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;//Gmail port number
            client.UseDefaultCredentials = true;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("yourid@gmail.com", "yourpassword");
            client.Send(message);
            SqlCommand cmd1 = new SqlCommand("insert into tblRandom values('" + txtemail.Text + "','" + random.ToString() + "')", con);

            cmd1.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('Security Code Successfully Sent in Your Email Id')</script>");

            Response.Redirect("~/anotherpage.aspx");
        }
        catch (Exception)
        {
           // Response.Write(ee.Message);
            lblmsg.Text = "Please Enter Email-Id..";
            lblmsg.Visible = true;
            //MessageBox.Show("Please Enter Email-ID");
            //Response.Write("<script>alert('Please Enter Email-ID')</script>");
        }
    }
    else
    {
        lblmsg.Text = "Please Enter Correct Email-Id..";
        lblmsg.Visible = true;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM