1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python调用foxmail 发邮件_调用foxmail发送邮件

python调用foxmail 发邮件_调用foxmail发送邮件

时间:2020-11-14 21:53:09

相关推荐

python调用foxmail 发邮件_调用foxmail发送邮件

因为foxmail没有api调用接口,工作中又需要使用foxmail发送邮件(主要是附件)。在网上没有发现这方面的程序。特写一下自己写的一个示例

示例如下:

需要添加 using System.Runtime.InteropServices;

public class FoxmailHelper

{

[DllImport("user32.dll", EntryPoint = "FindWindow")]

private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "GetWindow")]

private extern static IntPtr GetWindow(IntPtr hWnd, uint nCmd);

[DllImport("user32.dll")]

static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("User32.dll", CharSet = CharSet.Auto)]

public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);

[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]

private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

const int WM_SETTEXT = 0x0C;

///

///

///

/// 收件人

/// 抄送

/// 密送

/// 主题

/// 附件

///

///

public static bool Send(string to, string cc, string bcc, string subject, List listAttrach, string content = "")

{

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

string foxMailFilePath ="";

//找到foxmail的文件路径

foreach (System.Diagnostics.Process p in processes)

{

if (p.ProcessName.Equals("Foxmail", StringComparison.OrdinalIgnoreCase))

{

foxMailFilePath = p.MainModule.FileName;

break;

}

}

if (string.IsNullOrEmpty(foxMailFilePath))

{

throw new Exception("FoxMail未启动或未安装!");

}

string attach = "";

string subjectText = "";

if (listAttrach != null)

{

listAttrach.ForEach(s =>

{

attach += $"{s} ";

subjectText += $"{System.IO.Path.GetFileNameWithoutExtension(s)},";

});

attach = attach.Trim(' ');

subjectText = subjectText.Trim(',');

}

ProcessStartInfo processStartInfo = new ProcessStartInfo(foxMailFilePath)

{

UseShellExecute = false,

CreateNoWindow = true,

RedirectStandardError = true,

RedirectStandardInput = true,

RedirectStandardOutput = true

};

if (!string.IsNullOrEmpty(attach))

{

processStartInfo.Arguments = attach;

}

else

{

processStartInfo.Arguments = $"mailto:{to}?cc={cc}&bcc={bcc}&subject={subject}&body={content}";

}

bool isOk = false;

Process process = new Process() { StartInfo = processStartInfo };

isOk = process.Start();

System.Threading.Thread.Sleep(1000);

StringBuilder sb = new StringBuilder();

StringBuilder sbtext = new StringBuilder();

IntPtr foxForm = FindWindow(null, $"{subjectText} - 写邮件"); //主窗体 TFoxComposeForm.UnicodeClass

GetClassName(foxForm, sb, 256);

IntPtr foxFrame = GetWindow(foxForm, 5); //TFoxComposeFrame.UnicodeClass

GetClassName(foxFrame, sb, 256);

IntPtr layoutManager = GetWindow(foxFrame, 5); //TLayoutManager

GetClassName(layoutManager, sb, 256);

//取得第一个子控件

IntPtr htmlEditor = GetWindow(layoutManager, 5); // TFoxHTMLEditor

GetClassName(htmlEditor, sb, 256);

//密送(下一个)

IntPtr bccEditer = GetWindow(htmlEditor, 2);

GetClassName(bccEditer, sb, 256);

SendMessage(bccEditer, WM_SETTEXT, IntPtr.Zero, bcc);

//抄送(下一个)

IntPtr ccEditer = GetWindow(bccEditer, 2);

GetClassName(ccEditer, sb, 256);

SendMessage(ccEditer, WM_SETTEXT, IntPtr.Zero,cc);

//收件人(下一个)

IntPtr toEditer = GetWindow(ccEditer, 2);

GetClassName(toEditer, sb, 256);

// GetWindowText(toIntPtr, sbtext, 256);

SendMessage(toEditer, WM_SETTEXT, IntPtr.Zero, to);

//主题

IntPtr subjectEditer = FindWindowEx(layoutManager, IntPtr.Zero, "TFMEdit.UnicodeClass", null);

GetClassName(subjectEditer, sb, 256);

SendMessage(subjectEditer, WM_SETTEXT, IntPtr.Zero, subject);

return isOk;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。