![](/img/trans.png)
[英]Why am I getting cannot convert from 'MimeKit.MimeMessage' to 'System.Net.Mail.MailMessage'?
[英]cannot convert from 'object' to 'System.Net.Mail.MailMessage'
怎么了?
我正在嘗試發送電子郵件,但問題標題中出現錯誤。 為什么不將對象轉換為“ System.Net.Mail.MailMessage”。
private object message;
protected void btnSend_Click(object sender, EventArgs e)
{
String TMess = txtMessageBody.Text;
String TEmail = txtEmail.Text;
String TSub = txtSubject.Text;
//this particular email server requires us to login so
//create a set of credentials with the relevent username and password
System.Net.NetworkCredential userpass = new System.Net.NetworkCredential();
userpass.UserName = "email";
userpass.Password = "password";
//ensure the smtp client has the newly created credentials
client.Credentials = userpass;
if (TSub == "")
{
System.Windows.Forms.MessageBox.Show("Error: Enter the message.");
}
else
{
//create a new email from REPLACE_WITH_USER@gmail.com to recipient@domain.com
MailMessage message = new MailMessage("helloworld@gmail.com", txtEmail.Text);
}
//set the subject of the message, and set the body using the text from a text box
message.Subject = txtSubject.Text;
message.Body = txtMessageBody.Text;
//send the message
client.Send(message);
//clear the message box
//the email has been sent - either by displaying a message (e.g. a literal) or by redirecting them to a 'Message sent' page
txtMessageBody.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
}
var client = new SmtpClient();
var message = new MailMessage("helloworld@gmail.com", txtEmail.Text);
var subject = txtSubject.Text;
var body = txtMessageBody.Text;
message.Subject = subject;
mail.Body = body;
client.Send(message);
絕對最小值應該可以正常工作。 嘗試一次將另一條代碼添加到一行,看看它在哪里中斷。
問題出在您的if else循環中。 如果轉到if語句(而不是else語句),則您的mailmessage對象不存在。 不存在的內容無法解析。
you can do it like this MailMessage message = new MailMessage("helloworld@gmail.com", txtEmail.Text if (TSub == "") { System.Windows.Forms.MessageBox.Show("Error: Enter the message."); return; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.