使用带有gmail smtp的C#发送电子邮件,使用不同的电子邮件 [英] Sending email using C# with gmail smtp, with different email

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

问题描述

亲爱的所有


我使用gmail smtp发送电子邮件(gmail地址: no- reply_jay-company@gmail.com和密码= ???? )使用以下源代码,它工作正常。


当我从" thomas111@hotmail.com" ;,在接收方"来自电子邮件"字段包含  thomas111@hotmail.com< no-reply_jay-company@gmail.com>


 





何时    接收方想通过点击"回复"来回复该电子邮件。链接
然后 
no-reply_jay-company@gmail.com 插入  " to email "字段





我想要当接收者点击" 回复"链接  然后  thomas111@hotmail.com
要插入"到电子邮件"字段而不是"no-reply @ ...."





 


 


 


  public   class  MailSender 
{
public 静态 bool SendEmail(
string 来自 / * thoms111@hotmail.com * /
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
尝试
{
System.Web.Mail.MailMessage myMail = new 系统。 Web.Mail.MailMessage();
myMail.Fields.Add
" http://schemas.microsoft.com/cdo/configuration/smtpserver"
" smtp.gmail.com" );
myMail.Fields.Add
" http://schemas.microsoft.com/cdo/configuration/smtpserverport"
" 465" );
myMail.Fields.Add
" http://schemas.microsoft.com/cdo/configuration/sendusing"
" 2" );
// sendusing:cdoSendUsingPort,value 2,用于发送消息
//网络。

// smtpauthenticate:指定验证时使用的机制
//到SMTP
//通过网络提供的服务。可能的值为:
// - cdoAnonymous,值0.不进行身份验证。
// - cdoBasic,value 1.使用基本的明文身份验证。
//使用此选项时,您必须提供用户名和密码
//通过sendusername和sendpassword字段。
// - cdoNTLM,值2.当前进程安全上下文用于
//使用服务进行身份验证。
myMail.Fields.Add
" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" " 1" );
//使用0表示匿名
myMail.Fields.Add
" ; http://schemas.microsoft.com/cdo/configuration/sendusername"
" no-reply_jay-company@gmail.com" );
myMail.Fields.Add
" http://schemas.microsoft.com/cdo/configuration/sendpassword"
" ??????????????");
myMail.Fields.Add
" http://schemas.microsoft.com/cdo/configuration/smtpusessl"
" true" );
myMail.From = " thoms111@hotmail.com" ;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim()!= ""
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}

System.Web.Mail.SmtpMail.SmtpServer = " smtp.gmail.com:465" ;
System.Web.Mail.SmtpMail.Send(myMail);

myMail = null ;

return true ;
}
catch (Exception ex)
{
Sprang.General.Library.throwException(ex.Message);
}
返回 false ;
}
}

解决方案

这似乎是gmail上的配置问题。 Gmail根本不允许您发送与您的登录地址不同的电子邮件地址。 (这是一个很好的设置,否则我们会收到很多人欺骗来自地址的垃圾邮件!)


另一个问题:MailMessage实现IDisposable - 你应该处理MailMessage实例(试试看) / finally block 或using关键字)。在变量超出范围之前设置null的引用也根本不需要。


有亲切的问候,


Konrad


Dear All

I am sending email using gmail smtp (gmail address:no-reply_jay-company@gmail.com and password=????) using following source code, it works fine.

When I send email from "thomas111@hotmail.com", on receiver side "from email" field contains thomas111@hotmail.com <no-reply_jay-company@gmail.com>

 


when  receiver want to reply that email by clicking on "reply" link then no-reply_jay-company@gmail.com inserting to "to email" field.


I want when receiver click on "reply" link then thomas111@hotmail.com to be inserted in "to email" field instead of "no-reply@...."


 

 

 

 public class MailSender
    {
        public static bool SendEmail(
            string from /*thoms111@hotmail.com*/,
            string pTo,
            string pSubject,
            string pBody,
            System.Web.Mail.MailFormat pFormat,
            string pAttachmentPath)
        {
            try
            {
                System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                                  "smtp.gmail.com");
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                                  "465");
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                                  "2");
                //sendusing: cdoSendUsingPort, value 2, for sending the message using 
                //the network.

                //smtpauthenticate: Specifies the mechanism used when authenticating 
                //to an SMTP 
                //service over the network. Possible values are:
                //- cdoAnonymous, value 0. Do not authenticate.
                //- cdoBasic, value 1. Use basic clear-text authentication. 
                //When using this option you have to provide the user name and password 
                //through the sendusername and sendpassword fields.
                //- cdoNTLM, value 2. The current process security context is used to 
                // authenticate with the service.
                myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //Use 0 for anonymous
                myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                    "no-reply_jay-company@gmail.com");
                myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                     "??????????????");
                myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                     "true");
                myMail.From = "thoms111@hotmail.com";
                myMail.To = pTo;
                myMail.Subject = pSubject;
                myMail.BodyFormat = pFormat;
                myMail.Body = pBody;
                if (pAttachmentPath.Trim() != "")
                {
                    MailAttachment MyAttachment =
                            new MailAttachment(pAttachmentPath);
                    myMail.Attachments.Add(MyAttachment);
                    myMail.Priority = System.Web.Mail.MailPriority.High;
                }

                System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
                System.Web.Mail.SmtpMail.Send(myMail);

                myMail = null;

                return true;
            }
            catch (Exception ex)
            {
                Sprang.General.Library.throwException(ex.Message);
            }
            return false;
        }
    }

解决方案

That seems to be an configuration issue on gmail. Gmail simply does not allow you to send emails with different from address than your login address. (Which is a good setting else we would receive a lot of spam of people spoofing the from address!)

And another issue: MailMessage implements IDisposable - you should dispose MailMessage instances (using a try/finally block or the using keyword). And setting a reference to null before the variable goes out of scope is also not required at all.

With kind regards,

Konrad


这篇关于使用带有gmail smtp的C#发送电子邮件,使用不同的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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