我想要一个程序,需要一个字符串,并将其发送到电子邮件给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);
}
}
}
}
尝试调试。你会看到你的错误的悲剧。也就是说,不从'send'(唉)返回MailMessage,也不会将它传递给'smtp'(lawd),所以你可以*实际发送它*。 – Will
你有2个'MailMessage'。 –
真是一团糟。 send方法不发送任何东西。 smtp消息尝试发送未初始化的消息。您需要完整重构此代码。 –