Here a complete listing of the class module. I'm beginning to think it an encapsulation issue, i.e. needs to be inside <HTML></HTML> since that's the message format.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.IO;
namespace Gulfstream
{
public partial class Gulfstream : Form
{
string to = "";
string from = "";
string subject = "";
string body = @"";
string server = "";
int port;
string login = "";
string password = "";
public Gulfstream()
{
InitializeComponent();
ReadFile();
}
public void ReadFile()
{
//string DefaultValuesPath = @"DefaultValues.txt";
string DefaultValuesPath = Environment.CurrentDirectory + @"\\DefaultValues.txt";
// Open the stream and read it back.
FileStream fs = File.OpenRead(DefaultValuesPath);
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
{
string strDefaultData = temp.GetString(b);
string CurChar = "";
//string StrToWrite = "";
int Instance = 0;
int OldPos = 1;
int CurPos = 2;
int LastPos = strDefaultData.Length;
while (CurPos < LastPos)
{
CurChar = strDefaultData.Substring(CurPos, 1);
if (CurChar == "=")
{
Instance++;
switch (Instance)
{
case 1:
// SMTP Server
this.txtServer.Text = strDefaultData.Substring(OldPos, (CurPos - 2) - OldPos);
OldPos = CurPos;
break;
case 2:
// SMTP Port
this.txtPort.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 3:
// Login ID
this.txtSerLogin.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 4:
// Login password
this.txtSerPassword.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 5:
// From email address
this.txtFrom.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 6:
// Subject
this.txtSubject.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 7:
// Body
this.txtMessage.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
default:
break;
}
}
CurPos++;
}
}
}
fs.Close();
}
public void WriteFile()
{
//String StrToWrite = "=" + this.txtServer.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSerLogin.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSerPassword.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtFrom.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSubject.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtMessage.Text + //r//n + "=";
//string path = @"DefaultValues.txt";
string DefaultValuesPath = Environment.CurrentDirectory + @"\\DefaultValues.txt";
//delete old file first
if (File.Exists(DefaultValuesPath))
{
File.Delete(DefaultValuesPath);
}
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
StreamWriter sw = new StreamWriter(DefaultValuesPath);
// Add some text to the file.
// sw.Write("This is the ");
sw.WriteLine("=" + this.txtServer.Text);
sw.WriteLine("=" + this.txtPort.Text);
sw.WriteLine("=" + this.txtSerLogin.Text);
sw.WriteLine("=" + this.txtSerPassword.Text);
sw.WriteLine("=" + this.txtFrom.Text);
sw.WriteLine("=" + this.txtSubject.Text);
sw.WriteLine("=" + this.txtMessage.Text);
sw.WriteLine("=");
sw.Close();
}
public void CreateTestMessage1(string server, int port)
{
try
{
MailMessage message = new MailMessage(from, to, subject, body);
//MailMessage message = new MailMessage(from, to);
// information
message.Subject = subject;
message.IsBodyHtml = true;
// message.Body = body + "<div style=\"font-family:Arial\"><br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /></div>";
message.Body = "<body>" + body + "<div style=\"font-family:Arial\"><br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /></div>" + "</body>";
string attachmentPath = Environment.CurrentDirectory + @"\\sig.jpg";
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(attachmentPath);
string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/jpg";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
message.Attachments.Add(inline);
// replace the tag with the correct content ID
message.Body = message.Body.Replace("@@IMAGE@@", "cid:" + contentID);
SmtpClient client = new SmtpClient(server, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = new System.Net.NetworkCredential(login, password);
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
lblError.Text = "Email sent!";
}
catch (Exception e)
{
lblError.Text = e.Message.ToString();
}
}
private void butSave_Click(object sender, EventArgs e)
{
}
private void butExit_Click(object sender, EventArgs e)
{
}
private void butRun_Click(object sender, EventArgs e)
{
lblError.Text = "";
int warn = 0;
if (txtTo.Text == "")
{
warn = 1;
}
else if(txtFrom.Text == "")
{
warn = 1;
}
else if (txtSerPassword.Text == "")
{
warn = 1;
}
else if (txtSubject.Text == "")
{
warn = 1;
}
else if (txtPort.Text == "")
{
warn = 1;
}
else if (txtSerLogin.Text == "")
{
warn = 1;
}
else if (txtMessage.Text == "")
{
warn = 1;
}
int first = 0;
first = txtFrom.Text.IndexOf("@gulfstream.com");
if (first == 0)
{
warn = 2;
}
if (warn == 1)
{
lblError.Text = "All fields must be completed!";
}
else if (warn == 2)
{
lblError.Text = "For use by Gulfstream employees only!";
}
else
{
to = txtTo.Text;
from = txtFrom.Text;
login = txtSerLogin.Text;
password = txtSerPassword.Text;
subject = txtSubject.Text;
// loop through body text looking for carriage returns /n
int CurPos = 0;
string CurChar = "";
while (CurPos < txtMessage.Text.Length)
{
CurChar = txtMessage.Text.Substring(CurPos, 1);
if (CurChar == "=")
{
//body += "%0A" + txtMessage.Text.Substring(CurPos, 1);
//body += "<CF><LF>" + " " +txtMessage.Text.Substring(CurPos, 1);
//body += "'CRLF:\r\nPost CRLF'" + txtMessage.Text.Substring(CurPos, 1);
//body += "<br/>" + txtMessage.Text.Substring(CurPos, 1);
//body += "'\r\n'" + txtMessage.Text.Substring(CurPos, 1);
//body += ":::*" + txtMessage.Text.Substring(CurPos, 1);
//body += "%0d%0a" + txtMessage.Text.Substring(CurPos, 1);
//body += "%0d%0a" + txtMessage.Text.Substring(CurPos, 1);
//body += "%0D%0A" + txtMessage.Text.Substring(CurPos, 1);
body += Environment.NewLine + "%0D%0A" + txtMessage.Text.Substring(CurPos, 1);
}
else
{
body += txtMessage.Text.Substring(CurPos, 1);
}
CurPos++;
}
//body = txtMessage.Text;
server = txtServer.Text;
port = Int32.Parse(txtPort.Text);
CreateTestMessage1(server, port);
}
}
private void butExit_Click_1(object sender, EventArgs e)
{
this.Dispose();
}
private void butSave_Click_1(object sender, EventArgs e)
{
WriteFile();
lblError.Text = "Default values updated!";
}
private void butLoad_Click(object sender, EventArgs e)
{
ReadFile();
}
private void txtTo_TextChanged(object sender, EventArgs e)
{
}
private void pnlSMTP_Paint(object sender, PaintEventArgs e)
{
}
private void lblSerLogin_Click(object sender, EventArgs e)
{
}
}
}
The output...