我在发送电子邮件时遇到问题



我试图发送一封电子邮件,其中的设置来自文本文件。我被困在smtp部分的PORT部分。我好像不能用数字";26〃;的文本文件(因为它是int?(谢谢你(是的,我是C#的初学者(

text文件条目:

mymail@myserver.com
myemail@myprovider.com
passwort123
Randy
myemailsmtpprovider.com
false
26

这是我尝试的:

private void SendEmail(string file)
{
try
{
string[] content = File.ReadAllLines(@"settings.txt");
var fromAddress = new MailAddress(content[0], content[3]);
var toAddress = new MailAddress(content[0], content[3]);
string fromPassword = content[2].ToString();
const string subject = "Alarm Snapshot";
const string body = "Motion detected";
var attachmentFilename = file;
var attachment = new Attachment(attachmentFilename,  MediaTypeNames.Application.Octet);
if (attachmentFilename != null)
{
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = new FileInfo(attachmentFilename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
}
var smtp = new SmtpClient
{

Host = content[4],
Port =26, 
// here is the proble. i want to use the entriy content[6] 
// from the textfile instead of write 26 directly

EnableSsl = false,
// same with Ssl, i can not use the entry from the textfile
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
message.Attachments.Add(attachment);
smtp.Send(message);
InvokeGuiThread(() => label_Email.Text = @"Sending successful");
}
}
catch (Exception exception)
{
MessageBox.Show(@"Error: " + exception.Message);
}
}

问题解决了!

这两行必须(解析(转换:

Port = content[6],
EnableSsl = content[5],

进入

Port = int.Parse(content[6]),
EnableSsl = bool.Parse(content[5]),

最新更新