2

好吧,我已经完成了我的功课,甚至查看了这个网站上的几个示例,但无济于事。我的程序旨在发送表格上填写的数据,并将其发送到电子邮件。我的代码的其余部分没有显示任何错误,除了 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();   
      }
4

3 回答 3

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 。

于 2012-10-02T01:08:30.737 回答
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);
                }


            }

            }
        }
于 2012-10-02T01:38:30.013 回答
1

.NET中没有StmpMailMessage类。你想要MailMessage。您还希望将服务器凭据传递给SmtpClient.

此处的此问题说明了如何使用 Gmail 执行此操作。

使用 C# 通过 Gmail SMTP 服务器发送电子邮件

于 2012-10-02T01:08:07.913 回答