我试图创建一个带字符串的程序,然后通过电子邮件将其发送到gmail,然后gmail将其作为短信发送到手机。
我很确定我的所有代码都很好,但它一直在说#34;必须指定来自地址的#34;当我确实指定它。有人可以指出错误或只是给我修改代码吗?
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 = "number@vtext.com";
//static string from1 = "myemail@gmail.com";
//static string from2 = "Test";
static string email = "myemail@gmail.com";
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("myemail@gmail.com", "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);
}
}
}
}
答案 0 :(得分:0)
只需使用像这样的
mail.From = "myemail@gmail.com"
答案 1 :(得分:0)
这里有很多事情看起来不对劲。所以我只是重写它以使它工作。
LBBlurredImage
答案 2 :(得分: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 = "number@vtext.com";
static string from = "myemail@gmail.com";
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();
}
}
}
答案 3 :(得分:0)
如果您查看我的原始答案,您将看到需要将端口更改为587.
答案 4 :(得分:0)
如果有人使用MVC + Postal并且您正在从视图中选择电子邮件正文,请检查您的电子邮件视图文件夹中是否有_ViewStart.cshtml文件,该文件会覆盖默认的_Layout.cshtml
@{ Layout = null; }