2015-06-10 48 views
-2

我想要一个程序,需要一个字符串,并将其发送到电子邮件给gmail,然后将其发送到手机作为文本消息。它说:“一个地址必须指定”当我有它

我很确定我所有的代码都很好,但是当我指定它时,它总是说“必须指定一个来自地址的地址”。有人可以指出错误,或只是给我修改代码?

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

namespace ConsoleApplication1 
{ 
class Program 
{ 
    static string msg = ""; 
    static string to = "[email protected]"; 
    //static string from1 = "[email protected]"; 
    //static string from2 = "Test"; 
    static string email = "[email protected]"; 
    static string password = "password"; 
    static MailMessage smtpMSG = new MailMessage(); 

    static void Main(string[] args) 
    { 
     Console.WriteLine("Type in the message"); 
     msg = Console.ReadLine(); 
     Console.WriteLine("Click Enter to continue"); 
     if (Console.ReadKey().Key == ConsoleKey.Enter) 
     { 
      smtp(email, password); 
      send(msg, to); 
     } 
     Console.Read(); 
    } 

    static void send(string x, string y) 
    { 
     MailMessage message = new MailMessage(); 
     message.To.Add(new MailAddress(y)); 
     message.From = new MailAddress("[email protected]", "password"); 
     message.Body = x; 
    } 

    static void smtp(string x, string y) 
    { 
     SmtpClient smtp = new SmtpClient(); 
     smtp.EnableSsl = true; 
     smtp.Port = 25; 
     smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
     smtp.Credentials = new System.Net.NetworkCredential(x, y); 
     smtp.Host = "smtp.google.com"; 
     smtpMSG.Body = msg; 

     try 
     { 
      smtp.Send(smtpMSG); 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 
} 

}

+2

尝试调试。你会看到你的错误的悲剧。也就是说,不从'send'(唉)返回MailMessage,也不会将它传递给'smtp'(lawd),所以你可以*实际发送它*。 – Will

+0

你有2个'MailMessage'。 –

+0

真是一团糟。 send方法不发送任何东西。 smtp消息尝试发送未初始化的消息。您需要完整重构此代码。 –

回答

0

有这么多东西怎么回事,不看的权利。所以我只是重写它,使其工作。

class Program 
{ 
    const string to = "[email protected]"; 
    const string email = "[email protected]"; 
    const string password = "password"; 
    const string SMTPAddress = "smtp.gmail.com"; 
    const int SMTPPort = 587; 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Type in the message"); 
     string msgBody = Console.ReadLine(); 
     Console.WriteLine("Sending Message..."); 
     SendMsg(msgBody); 
     Console.WriteLine("Click Enter to continue"); 
     Console.Read(); 
    } 

    static MailMessage InitMailMessage(string msgBody) 
    { 
     MailMessage message = new MailMessage(); 
     message.To.Add(new MailAddress(to)); 
     message.From = new MailAddress(email, "The Displayed Name Not The Password"); 
     message.Body = msgBody; 
     return message; 
    } 

    static void SendMsg(string msgBody) 
    { 
     MailMessage msg = InitMailMessage(msgBody); 

     SmtpClient smtp = new SmtpClient(); 
     smtp.EnableSsl = true; 
     smtp.Port = SMTPPort; 
     smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
     smtp.Credentials = new System.Net.NetworkCredential(email, password); 
     smtp.Host = SMTPAddress; 
     try 
     {   
      smtp.Send(msg); 
     } 
     catch (Exception ex) 
     { 
      ex.Dump(); 
      Console.WriteLine(ex.Message); 
     } 
    } 

} 
0

我发现smtp.google.com不存在了,它已被更改为smtp.gmail.com。这解决了我的问题,但现在它只是挂起,直到超时(超时设置为30秒)才会执行任何操作。此外,什么是TLS,因为谷歌可能需要它,因为他们的安全性得到加强。

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
    static string msg = ""; 
    static string password = "password"; 
    static string to = "[email protected]"; 
    static string from = "[email protected]"; 

    static void Main(string[] args) 
    { 
     Console.WriteLine("Type in your message"); 
     msg = Console.ReadLine(); 
     MailMessage message = new MailMessage(); 
     message.To.Add(to); 
     message.From = new MailAddress(from); 
     message.Body = msg; 

     SmtpClient smtp = new SmtpClient(); 
     smtp.EnableSsl = true; 
     smtp.Port = 465; 
     smtp.Timeout = 30000; 
     smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
     smtp.Credentials = new System.Net.NetworkCredential(from, password); 
     smtp.Host = "smtp.gmail.com"; 


     try 
     { 
      Console.WriteLine("message sending..."); 
      smtp.Send(message); 
      Console.WriteLine("message sent"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 

     Console.Read(); 
     } 
    } 
} 
0

如果你看看我原来的答案,你会发现你需要你的端口更改为587

+0

587不起作用,谷歌不会接受它,所以你需要使用465并启用Ssl。 – smelborp

+0

另外,什么是'ex.Dump();' – smelborp

+0

这是一个实用功能,你可以删除它 – WellerEE

0

如果有人使用MVC +邮政和你一个是从视图选择电子邮件的主体,请检查您的电子邮件视图文件夹中是否有_ViewStart.cshtml文件,该文件会覆盖默认的_Layout.cshtml

@{ Layout = null; } 
相关问题