使用localhost发送电子邮件 [英] sending email using localhost

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

问题描述





在我的网站上,我从localhost发送电子邮件。

如果我指定了电子邮件地址给谁(例如; someone@gmail.com)我想发送成功的电子邮件,但我想从文本框控件中检索(var toAddress)值。

如果我这样做,它会给出错误:

指定的字符串不是电子邮件地址所需的格式。



发送电子邮件的代码如下所示:

  var  fromAddress =  new  MailAddress (  myid@gmail.com  sarwar); 
var toAddress = new MailAddress( GetValueFrom文本框 name); // 这里我想从文本框中获取值。

const string fromPassword = password;
const string subject = lokman;
const string body = 我已经完成了;

var smtp = new SmtpClient
{
Host = smtp.gmail.com
Port = 587
EnableSsl = true
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false
Credentials = new NetworkCredential(fromAddress.Address,fromPassword)
};

使用 var message = new MailMessage(fromAddress,toAddress)
{
Subject = subject,
Body = body
})
尝试
{
smtp.Send(message);
}

解决方案

  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 ;
}
// 将值传递给smtp对象
smtp。发送(fromAddress,toAddress,subject,body);





试试这段代码,如果它不起作用,请告诉我...... / blockquote>

  public   void  Messegesend( string email,string firstname,string lastname,string messege)
{
try
{

string messegebody = 亲爱的管理员 + \ n + messege + \ n谢谢\ n + firstname + + lastname + \ n +电子邮件;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient( smtp .gmail.com);
mail.From = new MailAddress( bizstallbevarage@gmail.com);
mail.To.Add( dipcse08@gmail.com);
mail.Subject = Bizstall Bevarage& Food;
mail.Body = messegebody;



SmtpServer.Port = 587 ;
SmtpServer.Credentials = new System.Net.NetworkCredential( sender_email sender_password);
SmtpServer.EnableSsl = true ;
SmtpServer.Send(mail);
}
catch (例外情况)
{
string error = ex.ToString();
}
}


假设您的用户可以输入,您所要做的就是执行以下行:

 toAddress =  new  MailAddress(myTextbox.Text); 

在创建新的之前MailMessage。



错字 - 忘记更改固定字符串以使用文本框内容。[/ edit]


Hi,

In my website I am sending email from localhost.
If I specify the email address to whom(e.g.;someone@gmail.com) I want to send email it succeeds, but I would like to retreive the (var toAddress) value from the text box control.
If i do this it gives the error:
"The specified string is not in the form required for an e-mail address."

The code to send a email looks like this:

var fromAddress = new MailAddress("myid@gmail.com", "sarwar");
var toAddress = new MailAddress("GetValueFrom The Textbox","name");// Here I would like to get value from the text box.

const string fromPassword = "password"; 
const string subject = "lokman";
const string body = "i have done it";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };

using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
try
{
    smtp.Send(message);
}

解决方案

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, toAddress, subject, body);



Try this code, If its not working, let me know...


public void Messegesend(string email,string firstname,string lastname,string messege)
       {
           try
           {

               string messegebody = "Dear Admin  " + "\n" + messege + "\n Thanks \n " + firstname + " " + lastname + " \n" + email;
               System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
               SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
               mail.From = new MailAddress("bizstallbevarage@gmail.com");
               mail.To.Add("dipcse08@gmail.com");
               mail.Subject = "Bizstall Bevarage & Food";
               mail.Body = messegebody;



               SmtpServer.Port = 587;
               SmtpServer.Credentials = new System.Net.NetworkCredential("sender_email", "sender_password");
               SmtpServer.EnableSsl = true;
               SmtpServer.Send(mail);
           }
           catch (Exception ex)
           {
               string error = ex.ToString();
           }
       }


Assuming your users can type, all you have to do is execute the line:

toAddress = new MailAddress(myTextbox.Text);

before you create the new MailMessage.

[edit]Typo - forgot to change fixed string to use textbox content.[/edit]


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

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