通过C#编码发送电子邮件 [英] email sending through c# coding

查看:122
本文介绍了通过C#编码发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的家庭电子邮件客户端编写代码,该代码不会在每次我应该发送电子邮件时都绑定我打开gmail帐户.
下面是源代码:

I was writing codes for my home email client which does not bind me to open gmail account every time when I am supposed to send the email.
Below is source codes:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;

namespace Emailclient 
{
    class Program
    {
        static void Main(string[] args)
        {
            string sender, receipents;
            MailMessage msgobj = new MailMessage();
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
           // client.Host="www.yahoo.com";
            //client.Port = 587;
            client.EnableSsl = true;
            Console.WriteLine(".................Email Client Version 1.0.0.0.........");
            Console.Write(".................Printing the Service point protocol version........");
            Console.Write(client.ServicePoint.ProtocolVersion.ToString());
            if (client.Credentials == null)
            {
                //Console.WriteLine("..............Credentials are set to null............");
                // username and password Is set as ceredntial for connecting to smtp accesss to gmail account
                client.Credentials = new System.Net.NetworkCredential("**your email id", "password");

            }
            Console.WriteLine("..............Enter your email address here and press Enter..........");
            try
            {
                // sender's emaill address .......
               // sender = Console.ReadLine();
               msgobj.From = new MailAddress("niraj.1991@gmail.com");
               Console.ForegroundColor = ConsoleColor.Red;
               Console.BackgroundColor = ConsoleColor.White;
               Console.WriteLine("Sending mail on the behalf of niraj.1991@gmail.com");
            }
            catch (FormatException fe)
            {
                Console.WriteLine("Following Error Ocured.......\n" + fe.Message);
                Console.Clear();
                Console.WriteLine("..............Enter your email address here and press Enter..........");
               sender= Console.ReadLine();
               msgobj.From = new MailAddress(sender);
            }
            Console.WriteLine("Enter your friend's email address here and press Enter..........");
            try 
            {
                // emaill addresses of recepients goes here........
                Console.ForegroundColor = ConsoleColor.Gray;

                Console.BackgroundColor = ConsoleColor.DarkCyan;
                receipents = Console.ReadLine();
               msgobj.To.Add ( receipents);
            }
            catch (FormatException fe2)
            {
                Console.WriteLine("Following Error Ocured.......\n" + fe2.Message);
                Console.Clear();
                Console.WriteLine("Enter your friend's email address here and press Enter..........");
                Console.ForegroundColor=ConsoleColor.Red;
                Console.WriteLine("Following Error Ocured.......\n" + fe2.Message);
                Console.Read();
               // Console.WriteLine("Press esc key to exit");
                
            }
            
            Console.WriteLine(".........Enter subject of the message.......");
            msgobj.Subject = Console.ReadLine().ToString();
           // msgobj.Attachments =new AttachmentCollection("C:\\Documents and Settings\\NIRAJ KUMAR CHAUHAN\\Desktop\\exception");
           // msgobj.BodyEncoding=System.Text.Encoding.ASCII;
            msgobj.IsBodyHtml=true;
            //TransferEncoding.Base64;
       
           msgobj.Priority=MailPriority.High;
            Console.WriteLine("........Enter your message body here...........");
            
            msgobj.Body = Console.Read().ToString();
            Console.WriteLine("..........Sending message to " + msgobj.To.ToString()+"..........");
            try
            {

                client.Send(msgobj);
                            
            }
            catch (SmtpException e)
            {
                Console.WriteLine(" ......Serious Error occured " + e.Message);
                Console.Read();
            }
            
            /*
                Console.WriteLine("Message send successfulluy.....");
            else
                Console.WriteLine("Mesaage sending failed......");*/
           
            Console.Read();
        }

      
    }
}


请在上面工作吗?
我曾发送过电子邮件,但有一个我无法解决的小问题.
问题在于电子邮件正文已编码为数字格式.
我不知道该怎么办.


Please work on above?
I worked email got send but there is slight problem which I couldn''t solve out.
The problem is email body got encoded into numeric formats.
I don''t understand what to do with this problem.

推荐答案

添加此行
msgobj.IsBodyHtml = true;


我已经尝试过以下但没有解决我的问题
I have tried the following but not got my problem solved
msgobj.IsBodyHtml=true;


这篇关于通过C#编码发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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