Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call QQ_Mail to send email in C #

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use QQ_Mail to send email by C #". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to call QQ_Mail to send mail".

Code case 1:

Private void button1_Click (object sender, EventArgs e) {string myMaillAdress = "; string myMaillPassword ="; string myMaillMessage = ""; string toMaillAdress = ""; QQEmail qq_mial = new QQEmail (); bool send_status = qq_mial.IsSendEmail (myMaillAdress, toMaillAdress, myMaillMessage, myMaillPassword) } class QQEmail {public bool IsSendEmail (string FromAddress, string ToAddress, string Message, string Pwd) {MailAddress Address = new MailAddress (FromAddress); MailMessage mail = new MailMessage (); mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Subject = "this is a message sent by" + FromAddress + ":" Mail.BodyEncoding = System.Text.Encoding.UTF8; mail.Body = Message; mail.IsBodyHtml = true; mail.Priority = System.Net.Mail.MailPriority.Low; mail.To.Add (ToAddress); mail.From = Address; SmtpClient smtp = new SmtpClient ("smtp.qq.com", 25) / / the server supported by smtp is smtp.qq.com, and the server port is 25587 or smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential (FromAddress, Pwd) / / remember that these two data must be filled in try {smtp.Send (mail); return true;} catch (Exception ex) {System.Windows.Forms.MessageBox.Show (ex.Message); return false }

Code case 2:

Using log4net;using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Mail;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Text;namespace BLL {public class emailHandle {private string _ serviceType = "SMTP"; private string _ host; / sender mailbox / public string From {get; set } / recipient mailbox list / public List To {get; set;} / public string [] Cc {get; set } / public string email list / public string [] Bcc {get; set;} / email subject / public string Subject {get; set;} / email content / public string Body {get; set } / is it in HTML format / public bool IsBodyHtml {get; set;} / attachment list / public string [] Attachments {get; set } / mailbox service type (Pop3,SMTP,IMAP,MAIL, etc.). Default is SMTP / public string ServiceType {get {return _ serviceType;} set {_ serviceType = value }} / Mailbox server. If no mailbox server is defined, the mailbox server / public string Host {get {return _ host;} set {_ host = value is formed according to serviceType and Sender. }} / mailbox account (default is sender mailbox account) / public string UserName {get; set;} / mailbox password (default is sender mailbox password), default format GB2312 / public string Password {get; set } / mailbox priority / public MailPriority MailPriority {get; set;} / email body encoding format / public Encoding Encoding {get; set } / Construction parameter, send email, use method Note: call / public int Send () {var mailMessage = new MailMessage () in the public method. / / read To recipient mailbox list try {if (this.To! = null & & this.To.Count > 0) {foreach (string to in this.To) {if (string.IsNullOrEmpty (to)) continue MailMessage.To.Add (new MailAddress (to.Trim () }} / / read Cc CC email address if (this.Cc! = null & & this.Cc.Length > 0) {foreach (var cc in this.Cc) {if (string.IsNullOrEmpty (cc)) continue MailMessage.CC.Add (new MailAddress (cc.Trim () }} / / read Attachments email attachment if (this.Attachments! = null & & this.Attachments.Length > 0) {foreach (var attachment in this.Attachments) {if (string.IsNullOrEmpty (attachment)) continue MailMessage.Attachments.Add (new Attachment (attachment)) }} / / read Bcc secret copywriter address if (this.Bcc! = null & & this.Bcc.Length > 0) {foreach (var bcc in this.Bcc) {if (string.IsNullOrEmpty (bcc)) continue MailMessage.Bcc.Add (new MailAddress (bcc.Trim ();}} / read From sender address mailMessage.From = new MailAddress (this.From); / / email title Encoding encoding = Encoding.GetEncoding ("GB2312") MailMessage.Subject = this.Subject; / / whether the email body is in HTML format mailMessage.IsBodyHtml = this.IsBodyHtml; / / email body mailMessage.Body = this.Body; mailMessage.BodyEncoding = this.Encoding; / / email priority mailMessage.Priority = this.MailPriority / / send email code to implement var smtpClient = new SmtpClient {Host = this.Host, EnableSsl = true, Credentials = new NetworkCredential (this.UserName, this.Password)} / / send an error message using the company mailbox before adding this paragraph: according to the verification process, the remote certificate is invalid / / plus the post-resolution problem ServicePointManager.ServerCertificateValidationCallback = delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {return true;}; / / Authentication smtpClient.Send (mailMessage); return 1 } catch (Exception ex) {var loger = LogManager.GetLogger (typeof (emailHandle)); loger.Info (string.Format ("send email exception, incoming mailbox: {0}", this.To [0]), ex); return-1 At this point, I believe you have a deeper understanding of "how to use QQ_Mail to send mail". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report