在Windows应用程序

在Windows应用程序

本文介绍了在Windows应用程序.net C#中发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好..我正在成功使用此代码,我得到了答案....但是我的朋友使用的是Gmail代码安全访问,例如sms alart.那时我可以访问NetworkCredential ...

hello .. i am using this code successfully i got answer....but my friend using gmail code security access like sms alart .in that time can i access NetworkCredential ...

 usrname = txt_username.Text;
                string mesg = "";
                mesg = txt_message.Text;
                string sub = txt_sub.Text;
                from = new MailAddress(txt_username.Text);
                to = new MailAddress(address);
                msg = new MailMessage(from, to);
                msg.Subject = sub;
                //msg.Attachments.Add(new Attachment(lbl_Attachment1.Text));
                msg.Body = mesg;
                System.Net.NetworkCredential network = new System.Net.NetworkCredential(txt_username.Text, txt_password.Text);
                smtp.Host = "smtp.gmail.com";
                    smtp.Port = 25;
                    smtp.EnableSsl = tru
e;
                smtp.Credentials = network;
                smtp.Send(msg);
                MessageBox.Show("sended");

推荐答案

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Security.Permissions;

public class SendMail
{
    string to;
    string subject;
    string message;
    string frm;
    public SendMail(string to, string subject, string message, string frm)
    {
        this.to = to;
        this.subject = subject;
        this.message = message;
        this.frm = frm;	
	}
    public bool Send()
    {
        //set id and password like this

        //string user = "[email protected]";
        //string password = "dassadad";

        //or like this
        string user = ConfigurationManager.AppSettings["username"].ToString();
        string password = ConfigurationManager.AppSettings["password"].ToString();
        SmtpClient SmtpServer = new SmtpClient();
        SmtpServer.Credentials = new System.Net.NetworkCredential(user, password);
        SmtpServer.Port = 587;
        SmtpServer.Host = "smtp.gmail.com";
        SmtpServer.EnableSsl = true;
        MailMessage mail = new MailMessage();
        try
        {

            mail.From = new MailAddress(user, "Subject", System.Text.Encoding.UTF8);
            mail.To.Add(new MailAddress(to));
            mail.Subject = subject;
            mail.Body = message;
            mail.IsBodyHtml = true;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mail.ReplyTo = new MailAddress(frm);
            SmtpServer.Send(mail);

        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }
}


MailAddress mailfrom = new MailAddress ( "[email protected]" );
MailAddress mailto = new MailAddress ( "[email protected]" );
MailMessage newmsg = new MailMessage ( mailfrom, mailto );

newmsg.Subject = "Subject of Email";
newmsg.Body = "Body(message) of email";

////For File Attachment, more file can also be attached

Attachment att = new Attachment ( "G:\\code.txt" );
newmsg.Attachments.Add ( att );

SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
smtps.UseDefaultCredentials = false;
smtps.Credentials = new NetworkCredential ( "[email protected]", "pwd" );
smtps.EnableSsl = true;
smtps.Send ( newmsg );


这篇关于在Windows应用程序.net C#中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:44