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);

ここでは、SmtpMailClient(送信を行うため)とメッセージを説明するMailMessageの2つのオブジェクトを使用しています。

于 2012-10-02T01:08:30.737 に答える
1

StmpMailMessage.NET にはクラスがありません。あなたがしたいMailMessage。また、サーバー資格情報を に渡したいと考えていますSmtpClient

This question here は、Gmail でそれを行う方法を説明しています。

C# を使用して Gmail SMTP サーバー経由でメールを送信する

于 2012-10-02T01:08:07.913 に答える
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 に答える