DOBON.NET DOBON.NETプログラミング掲示板過去ログ

SMTPでメールを送信する時

環境/言語:[WindowsXP / VB.NET]
分類:[.NET]

ここの「SMTPでメールを送信する」を見てやってみたんですが、エラーが起きました。
多分、パスワード・ユーザーIDが無いからだと思うのですが、
SMTPサーバーにログインするにはどうすればいいのですか?
サーバーからの応答は次のとおりです。
521 smtp.mail.yahoo.co.jp closing transmission channel.
You must be pop-authenticated before you can use this smtp server,
and you must use your yahoo mail address for the Sender/From field.?
  • 題名: Re[2]: 追加
  • 著者: EE
  • 日時: 2004/10/20 21:14:23
  • ID: 6880
  • この記事の返信元:
    • [6879] 追加 K 2004/10/20 20:15:38
  • この記事への返信:
  • ツリーを表示
SMTPサーバはプロバイダの物を使ってるの?
それだったら、メールソフトと同じ設定でOK。
> 521 smtp.mail.yahoo.co.jp closing transmission channel.
> You must be pop-authenticated before you can use this smtp server,
> and you must use your yahoo mail address for the Sender/From field.?

メッセージの内容をそのまま解釈すると、メールを送信する前に受信しろということでは?(俗に言う「POP before SMTP」のようですね。)

#このあたりはプロバイダに質問すべき内容だと思いますが...。
> SMTPサーバはプロバイダの物を使ってるの?
> それだったら、メールソフトと同じ設定でOK。

プロバイダではなく、ヤフーのフリーメールです。


>メールを送信する前に受信しろということでは?

試してみます。
2004/10/21(Thu) 20:43:23 編集(投稿者)

やってみました。ちゃんとPOPサーバーにアクセスしてからSMTPにアクセスするようにしました。(ここのHPのサンプルを流用して)
エラーはなさそうでしたが、ビルドして、送信しようとするとエラーが起こります。
2004/10/21(Thu) 20:43:45 編集(投稿者)

ソースです。

Imports System
Imports System.Text
Imports System.Net.Sockets

Public Class Form1

Private Overloads Shared Function ReceiveData( _
ByVal stream As NetworkStream, _
ByVal multiLines As Boolean, _
ByVal bufferSize As Integer, _
ByVal enc As Encoding) As String
Dim data(bufferSize - 1) As Byte
Dim len As Integer
Dim msg As String = ""
Dim ms As New System.IO.MemoryStream
Do
len = stream.Read(data, 0, data.Length)
ms.Write(data, 0, len)
msg = enc.GetString(ms.ToArray())
Loop While stream.DataAvailable Or _
((Not multiLines Or msg.StartsWith("-ERR")) And _
Not msg.EndsWith(vbCrLf)) Or _
(multiLines And Not msg.EndsWith(vbCrLf + "." + vbCrLf))
ms.Close()
If msg.StartsWith("-ERR") Then
Throw New ApplicationException("Received Error")
End If
Console.Write(("S: " + msg))
Return msg
End Function 'ReceiveData

Private Overloads Shared Function ReceiveData( _
ByVal stream As NetworkStream, _
ByVal multiLines As Boolean, _
ByVal bufferSize As Integer) As String
Return ReceiveData(stream, multiLines, bufferSize, _
Encoding.GetEncoding(50220))
End Function 'ReceiveData

Private Overloads Shared Function ReceiveData( _
ByVal stream As NetworkStream, _
ByVal multiLines As Boolean) As String
Return ReceiveData(stream, multiLines, 256)
End Function 'ReceiveData

Private Overloads Shared Function ReceiveData( _
ByVal stream As NetworkStream) As String
Return ReceiveData(stream, False)
End Function 'ReceiveData

Private Overloads Shared Sub SendData( _
ByVal stream As NetworkStream, _
ByVal msg As String, _
ByVal enc As Encoding)
Dim data As Byte() = enc.GetBytes(msg)
stream.Write(data, 0, data.Length)
Console.Write(("C: " + msg))
End Sub 'SendData

Private Overloads Shared Sub SendData(ByVal stream As NetworkStream, ByVal msg As String)
SendData(stream, msg, Encoding.ASCII)
End Sub 'SendData

Public Sub Main()
Dim mails() As String
mails = Receive("localhost", 110, "userid", "password", True)
Console.ReadLine()
End Sub 'Main

Public Function Receive(ByVal hostName As String, _
ByVal portNumber As Integer, ByVal userId As String, _
ByVal passWord As String, ByVal deleteMails As Boolean) As String()
Dim msg As String = ""
Dim mails() As String
Dim stream As NetworkStream
Dim client As New TcpClient
client.ReceiveTimeout = 10000
client.SendTimeout = 10000
client.Connect("pop.mail.yahoo.co.jp", 110)
stream = client.GetStream()
msg = ReceiveData(stream)
SendData(stream, "koutakkkkk" + userId + vbCrLf)
msg = ReceiveData(stream)
SendData(stream, "zaqzaqzaq" + passWord + vbCrLf)
msg = ReceiveData(stream)
SendData(stream, "STAT" + vbCrLf)
msg = ReceiveData(stream)
Dim mailsCount As Integer = _
Integer.Parse(msg.Split(" "c)(1))
mails = New String(mailsCount - 1) {}
SendData(stream, "QUIT" + vbCrLf)
msg = ReceiveData(stream)
client.Close()
Return mails
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Receive(pop_a.Text, port.Text, userid.Text, password.Text, False)
Dim mm As New System.Web.Mail.MailMessage()
Dim attachment As System.Web.Mail.MailAttachment
If address_s.Text = "" Then
Dim f As New Form2()
f.ShowDialog(Me)
Exit Sub
End If
If address_r.Text = "" Then
Dim f As New Form2()
f.ShowDialog(Me)
Exit Sub
End If
If smtp_a.Text = "" Then
Dim f As New Form2()
f.ShowDialog(Me)
Exit Sub
End If
mm.From = address_s.Text
mm.To = address_r.Text
mm.Subject = title.Text
mm.Body = maintext.Text
mm.BodyEncoding = System.Text.Encoding.GetEncoding(50220)
If yuusen.SelectedItem = "高い" Then
mm.Priority = Web.Mail.MailPriority.High
End If
If yuusen.SelectedItem = "普通" Then
mm.Priority = Web.Mail.MailPriority.Normal
End If
If yuusen.SelectedItem = "低い" Then
mm.Priority = Web.Mail.MailPriority.Low
End If
If CheckBox1.CheckState = CheckState.Checked Then
attachment = New System.Web.Mail.MailAttachment(OpenFileDialog1.FileName)
mm.Attachments.Add(attachment)
End If
Dim tcp As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient()
tcp.Connect("pop.mail.yahoo.co.jp", 110)
Dim sw As IO.StreamWriter = New IO.StreamWriter(tcp.GetStream())
sw.Write("USER userid.text\nPASS password.text\nSTAT\nQUIT\n")
System.Web.Mail.SmtpMail.SmtpServer = smtp_a.Text
System.Web.Mail.SmtpMail.Send(mm)
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.CheckState = CheckState.Checked Then
tenpfile.Enabled = True
Button2.Enabled = True
Else
tenpfile.Enabled = False
Button2.Enabled = False
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.ShowDialog()
tenpfile.Text = OpenFileDialog1.FileName
End Sub

End Class

DOBON.NET | プログラミング道 | プログラミング掲示板