发送邮件的Web服务不起作用 [英] Web Service for sending mails not working

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

问题描述

你好,我已经创建了一个用于发送邮件的网络服务,虽然一切似乎都没问题,而且.asmx正在运行,当我试图通过我的.aspx页面使用它时,它无法正常工作。你能不看看请。



网络服务

Hello, I have created a web service for sending mails and while everything seems to be ok and the .asmx is working, when Im trying to use it through my the .aspx page it is not working.Could you take a look please.

Web Service

public class mail : System.Web.Services.WebService {

    public mail () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public void SendMail(string mail,string subject, string body)
    {
        // Gmail Address from where you send the mail
        var fromAddress = "xnethardware@gmail.com";
        // any address where the email will be sending
        var toAddress = mail.ToString();
        //Password of your gmail address
        const string fromPassword = "xnet1234";
     
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, mail, subject, body);
    }
   
}





aspx.cs





aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class contact : System.Web.UI.Page
{



    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            localhost.mail minima = new localhost.mail();
            minima.SendMail(mail.Text, subject.Text, body.Text);
            DisplayMessage.Text = "Your Comments after sending the mail";
            DisplayMessage.Visible = true;
           
        }
        catch (Exception) { }
        
    }
}





感谢任何帮助,因为我刚接触编码。



Would appreciate any help since im new to coding.

推荐答案

bool SendMail (string strFrom, string strPass, string strTo, string strSub, string strBody)
	{
            SmtpClient smtpClient = new SmtpClient( "smtp.gmail.com", 465 );
            System.Net.NetworkCredential credentials = null ;
             bool bSuccess = true ;
            
            MailMessage message = new MailMessage();
            // Try to send the message
            try
            {
                // Prepare two email addresses 
                MailAddress fromAddress = new MailAddress(strFrom);
                MailAddress toAddress = new MailAddress (strTo);
                // Prepare the mail message
                message.From = fromAddress;
                message.To.Add(toAddress);
                message.Subject = strSub;
                message.IsBodyHtml = true;
                credentials = new System.Net.NetworkCredential( strFrom, strPass);
                smtpClient.Credentials = credentials;
                smtpClient.Send(message); 
            }
            catch ( Exception eX )
            {
                 // eX.Message shows  the cause of the error, if it failes.
                 bSuccess = false ;
            }
            return bSuccess ;
     }


我更新了项目中的所有Web服务,并重新添加了Web引用,一切正常
I updated all the web services in my project and re added web references and everything worked


这篇关于发送邮件的Web服务不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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