好吧,我已经完成了我的作业,甚至在这个网站上看了几个例子,但没有用。我的程序旨在发送填写在表单上的数据,并将其发送到电子邮件。除了SmptMailMessage之外,我的其余代码都没有显示任何错误。这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace FPSArrestReport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
SmtpClient SmptMailMessage = new SmtpClient();
SmptMailMessage mail = new SmtpMailMessage("smtp.gmail.com", 25); // error on this line
\\on the SmptMailmessage
//set the to address to the primary email
mail.To.Add("xxx@abc.com");
//set the message type and subject and body
mail.IsHtmlMessage = true;
mail.Subject = "";
mail.Body = "Hello world!";
//send the email
mail.Send();
}
答案 0 :(得分:5)
System.Net.Mail库中没有类型为SmtpMailMessage(或SmptMailMessage)。看起来您正在尝试创建一个SmtpClient实例来发送消息。也许你的意思是做类似的事情;
SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
MailMessage mail = new MailMessage();
mail.To.Add("xxx@abc.com");
client.Send(mail);
您在这里使用了2个对象 - 一个SmtpMailClient(用于发送)和一个描述该消息的 MailMessage 。
答案 1 :(得分:1)
.NET中没有StmpMailMessage
类。你想要MailMessage
。您还希望将服务器凭据传递给SmtpClient
。
此处的问题解释了如何使用Gmail。
答案 2 :(得分:1)
这应该是一个简单的解决方案。此外,尝试正确观察您的拼写。 :d
$
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
MailAddress fromAddress = new MailAddress("YourEmailAcct@gmail.com", "NameForIt");
MailAddress toAddress = new MailAddress("DesinationEmailAddress", "NameForDestination");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "First line of text \n Second line of text.";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient()
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
try
{
MailMessage message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
};
client.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("There was an error!" + ex.Message);
}
}
}
}